第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為列表中的 URL 運(yùn)行 python 腳本并輸出到 txt

為列表中的 URL 運(yùn)行 python 腳本并輸出到 txt

慕勒3428872 2021-12-21 10:33:06
我有一個(gè)用于單個(gè) URL 的 python 腳本,我需要為來自 url.txt 的多個(gè) URL 運(yùn)行它,并在單個(gè) txt 文件中獲取輸出。這是python腳本(縮?。篿mport urllib2from bs4 import BeautifulSoupquote_page = 'https://www.example.com/page/1024'#Rest of the script hereprint var1print var2print var3以下是一個(gè) URL 的示例輸出:Name: John DoeDOB: 01-Jan-1980Gender: Male我想要這個(gè) URL 1 的輸出,我的腳本完全按照我的意愿給出。我想在 url.txt 中對 URL 2、URL 3 等重復(fù)此操作。任何想法如何?PS 我一直保持簡單的問題,但如果你需要更多的細(xì)節(jié),讓我知道,我會(huì)這樣做。
查看完整描述

2 回答

?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

以追加模式打開一個(gè)文件,并將每個(gè)文件的輸出寫入其中。


import urllib2

from bs4 import BeautifulSoup

quote_page = 'https://www.example.com/page/1024'

#Rest of the script here

output = open("output.txt", 'a') # 'a' means open in append mode so the file is not overwritten

# change print to output.write()

output.write(str(var1) + '\n') # separate each var by a new line

output.write(str(var2) + '\n')

output.write(str(var3) + '\n')


output.close()

這將寫入所有 var1,然后是所有 var2,然后是所有 var3,每個(gè)都以空行分隔,然后關(guān)閉文件。


為了使其更兼容從命令行接受 url:


import sys

import urllib2

from bs4 import BeautifulSoup

quote_page = sys.argv[1] # this should be the first argument on the command line

#Rest of the script here

output = open("output.txt", 'a') # 'a' means open in append mode so the file is not overwritten

# change print to output.write()

output.write(str(var1) + '\n') # separate each var by a new line

output.write(str(var2) + '\n')

output.write(str(var3) + '\n')


output.close()

使用您的 url 的示例命令行:


$python3.6 myurl.py https://www.example.com/page/1024


查看完整回答
反對 回復(fù) 2021-12-21
?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

要從您的文件中獲取 url,您需要打開它,然后為每一行運(yùn)行您的腳本。假設(shè)每一行有一個(gè) url。要寫入輸出文件,請打開一個(gè)文件并將 var1、var2 和 var3 寫入其中


import urllib2

from bs4 import BeautifulSoup


with open('url.txt') as input_file:

    for url in input_file:

        quote_page = url

        #Rest of the script here


with open("ouput_file.txt", "w") as output:

    output.write(f'{var1}\n')

    output.write(f'{var2}\n')

    output.write(f'{var3}\n')


查看完整回答
反對 回復(fù) 2021-12-21
  • 2 回答
  • 0 關(guān)注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)