到目前为止,我的情况是:
import csv, re
from bs4 import BeautifulSoup as soup
import requests
flag = False
with open('filename.csv', 'w') as f:
write = csv.writer(f)
for i in range(38050, 38050): ##this is so I can test run with one page
s = soup(requests.get('https://howlongtobeat.com/game.php?id={i}').text, 'html.parser')
if not flag: #write header to file once
write.writerow(['Name', 'Length']+[re.sub('[:\n]+', '', i.find('strong').text) for i in s.find_all('div', {'class':'profile_info'})])
flag = True
## this is for if there is no page or an error
content = s.find('div', {"class":'profile_header shadow_text'})
if content:
name = s.find('div', {"class":'profile_header shadow_text'}).text
length = [[i.find('h5').text, i.find("div").text] for i in s.find_all('li', {'class':'time_100'})]
stats = [re.sub('\n+[\w\s]+:\n+', '', i.text) for i in s.find_all('div', {'class':'profile_info'})]
这不是写csv,也不知道为什么(我只是个初学者)
我试图创建一个循环来检查这些元素是否存在,如果存在,将它们写入hltb.csv文件'
我该怎么做?你知道吗
您可能需要增加for循环的值。你知道吗
此脚本将永远运行。你需要添加一些HTTP状态码404处理程序,以防你找不到任何这样的脚本可以结束。我认为你所做的是一个糟糕的方法,我宁愿访问网站菜单中的每个链接,并抓取任何与URL https://howlongtobeat.com/game.php?id=相关的内容,这样我就知道有限的URL应该在哪里查找,而不是猜测增量ID
你正在一个空范围内迭代。你知道吗
此范围的大小为0。尝试将最大值增加1。你知道吗
相关问题 更多 >
编程相关推荐