import requests
from bs4 import BeautifulSoup
import csv
domain = "http://www.100ppi.com/mprice/"
lis = [
"mlist-1--1.html"
"mlist-1--2.html"
"mlist-1--3.html"
"mlist-1--4.html"
"mlist-1--5.html"
"mlist-1--6.html"
"mlist-1--7.html"
"mlist-1--8.html"
"mlist-1--9.html"
"mlist-1--10.html"
]
f = open("大宗商品.csv", mode="a", newline='')
csvwriter = csv.writer(f)
for i in lis:
url = domain + i
try:
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3870.400 QQBrowser/10.8.4405.400"
}
resp = requests.get(url)
resp.raise_for_status()
resp.encoding = resp.apparent_encoding
#print(resp.text)
except:
print("F")
page = BeautifulSoup(resp.text, "html.parser")
shangping = page.find("div", attrs = {"class":"block clearfix ovh"})
trs = shangping.find_all("tr")[1:]
for tr in trs:
tds = tr.find_all("td")
name = tds[0].text.strip()
type = tds[1].text.strip()
brand = tds[2].text.strip()
price = tds[3].text.strip()
price_type = tds[4].text.strip()
adress = tds[5].text.strip()
bill = tds[6].text.strip()
time = tds[7].text.strip()
#print(name, type, brand, price, price_type, adress, bill, time)
csvwriter.writerow([name, type, brand, price, price_type, adress, bill, time])
f.close()
print("over!")
代码运行后在csv中得到的是最后一页的数据,求解答