我是Python的新手,我正在測驗我的第一個搜刮器(使用我在這里和那里找到的一些代碼)。我能夠用所有需要的資訊撰寫CSV,但是現在我試圖輸入一個以上的URL,而腳本只是寫我在陣列中插入的最后一個URL,這就像沒有追加新的URL,而只是在相同的第一個原始URL上重新寫。
我到處尋找,并嘗試了很多東西,但我想我需要一些幫助,謝謝!
我想我需要幫助。
from bs4 import BeautifulSoup
import requests
from csv import writer
urls = ['https://example.com/1'/span>, 'https://example.com/2'/span>]
for url in urls:
my_url = requests.get(url)
html = my_url.content
soup = BeautifulSoup(html,'html.parser')
資訊 = []
print (urls)
list = soup.find_all('div', class_="file-info-holder")
links = soup.find_all('a', class_="攔截")
with open('multi. csv', 'w', encoding='utf8', newline=' ') as f。
thewriter = writer(f)
header = ['Name', 'Location', 'Link', 'Link2', 'Link3']
thewriter.writerow(header)
for list in lists:
name = list.find('div', class_="file-name").text
location = list.find('div', class_="profile-location").text
social1 = links[0]
social2 = links[1]
social3 = links[2]
info = [name, location, social1.get('href'),social2.get('href'),social3.get('href') ]
thewriter.writerow(info)
uj5u.com熱心網友回復:
基本的方法
--
with open('multi. csv', 'a', encoding='utf8', newline=' ') as f。
你可能要用另一種方式來安排你的回圈,但如果沒有urls,就很難描述:
from bs4 import BeautifulSoup
import requests
from csv import writer
urls = ['https://example.com/1'/span>, 'https://example.com/2'/span>]
with open('multi. csv', 'a', encoding='utf8', newline=' ') as f。
thewriter = writer(f)
header = ['Name', 'Location', 'Link', 'Link2', 'Link3']
thewriter.writerow(header)
for url in urls:
my_url = requests.get(url)
html = my_url.content
soup = BeautifulSoup(html,'html.parser')
資訊 = []
lists = soup.find_all('div', class_="file-info-holder")
for l in lists:
name = l.find('div', class_="file-name").text
location = l.find('div', class_="profile-location").text
links = l.find_all('a', class_="攔截")
social1 = links[0]
social2 = links[1]
social3 = links[2]
info = [name, location, social1.get('href'),social2.get('href'),social3.get('href') ]
thewriter.writerow(info)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/310234.html
標籤:
