[英]Python crawl multiples URLs from a CSV and export to another CSV
我需要遍歷存儲在 CSV 文件中的 URL。 我想從列出的 URL 中提取電話和 ZIP。
請如果你能幫助我,我很感激!
# read csv with just url per line
with open('urls.csv') as file:
start_urls = [line.strip() for line in file]
def start_request(self):
request = Request(url = self.start_urls, callback=self.parse)
yield request
def parse(self, response):
html = response.body
soup = BeautifulSoup(html, 'lxml')
text = soup.get_text()
phone = re.findall(r'\d{3}-\d{3}-\d{4}', html, re.MULTILINE)
zipcode = re.findall(r'(?<=, [A-Z]{2} )\d{5}', html, re.MULTILINE)
phn_1 = []
zipcode_1 = []
´´´
您描述了您的目標,但沒有提及當前無法正常工作的部分。
你寫了這個:
def start_request(self):
request = Request(url=self.start_urls, callback=self.parse)
yield request
這不是你想要的。 特別是我希望 Request() 接受單個 url 而不是列表。 此外,使用回調很好,但可能比需要的更好。 試試這個簡化的方法:
for url in start_urls:
self.parse(Request(url=url))
我確信這個表達式對你很有效: [line.strip() for line in file]
。 為了強調這一切都是為了處理換行符,使用起來會更清楚
line.rstrip()
代替
line.strip()
感謝你的回答! 我可以循環,但是當我循環獲取帶有數據的 CSV 時,我無法獲取電話和 ZIP。 任何幫助我將不勝感激!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.