所以我试图从here中摘取标题。整整10年。你知道吗
years
是一个包含
/resources/archive/us/2007.html
/resources/archive/us/2008.html
/resources/archive/us/2009.html
/resources/archive/us/2010.html
/resources/archive/us/2011.html
/resources/archive/us/2012.html
/resources/archive/us/2013.html
/resources/archive/us/2014.html
/resources/archive/us/2015.html
/resources/archive/us/2016.html
因此,我的代码在这里所做的是,它打开每年的页面,收集所有的日期链接,然后单独打开每个链接,获取所有的.text
,并将每个标题和相应的日期作为一行添加到dataframe headlines
headlines = pd.DataFrame(columns=["date", "headline"])
for y in years:
yurl = "http://www.reuters.com"+str(y)
response=requests.get(yurl,headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', })
bs= BeautifulSoup(response.content.decode('ascii', 'ignore'),'lxml')
days =[]
links = bs.findAll('h5')
for mon in links:
for day in mon.next_sibling.next_sibling:
days.append(day)
days = [e for e in days if str(e) not in ('\n')]
for ind in days:
hlday = ind['href']
date = re.findall('(?!\/)[0-9].+(?=\.)', hlday)[0]
date = date[4:6] + '-' + date[6:] + '-' + date[:4]
print(date.split('-')[2])
yurl = "http://www.reuters.com"+str(hlday)
response=requests.get(yurl,headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', })
if response.status_code == 404 or response.content == b'':
print('')
else:
bs= BeautifulSoup(response.content.decode('ascii', 'ignore'),'lxml')
lines = bs.findAll('div', {'class':'headlineMed'})
for h in lines:
headlines = headlines.append([{"date":date, "headline":h.text}], ignore_index = True)
运行它需要很长时间,所以我没有运行for循环,而是运行了这一年/resources/archive/us/2008.html
已经三个小时了,它还在运行。你知道吗
由于我是Python新手,我不明白自己做错了什么,也不知道如何才能做得更好。你知道吗
会不会是pandas.append
因为每次运行都要读写一个更大的数据帧而花费了很长时间?你知道吗
您正在使用此反模式:
相反,请执行以下操作:
第二个潜在的问题是您正在发出3650个web请求。如果我经营这样一个网站,我会建立节流,以减缓像你这样的刮板。您可能会发现最好只收集一次原始数据,将其存储在磁盘上,然后在第二次处理中进行处理。这样就不会在每次需要调试程序时产生3650个web请求的开销。你知道吗
相关问题 更多 >
编程相关推荐