我已经从duckduckgo.com 抓取了结果并将结果存储在标题、链接、描述链接和描述被打印但标题没有被打印
我已经用 print(title) 打印了标题,它给出了输出
class DuckduckgoScraper(web_scraping):
def scrape(self,search_Term):
self.filename = search_Term
self.url = 'https://duckduckgo.com/html?q='+search_Term
r = requests.get(self.url,headers=USER_AGENT)
soup = BeautifulSoup(r.content,'html5lib')
result_block = soup.find_all(class_ = 'result__body')
for result in result_block:
link = result.find('a', attrs={'class':'result__a'}, href=True)
title = result.find('h2')
description = result.find(attrs={'class':'result__snippet'})
if link and title:
link = link['href']
title = title.get_text()
if description:
description = description.get_text()
with open(self.filename+'.csv', 'a', encoding='utf-8',newline='') as csv_file:
file_is_empty = os.stat(self.filename+'.csv').st_size==0
fieldname = ['title','link','description']
writer = csv.DictWriter(csv_file,fieldnames=fieldname)
if file_is_empty:
writer.writeheader()
writer.writerow({'title':title,'link':link,'description':description})
它没有给出任何错误