-2

我已经从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})

它没有给出任何错误

4

2 回答 2

0

您在每行迭代中打开 ng 并将其写入 csv 文件。.writerows()取而代之的是,将行存储在列表中,并在最后用函数一次写入它们。

注意:.strip()对行的每个项目执行此操作很有用,否则 Excel/LibreOffice/...在打开文件时可能会感到困惑。

import os
import csv
import requests
from bs4 import BeautifulSoup

USER_AGENT = {'User-Agent':'Mozilla/5.0'}

def scrape(search_Term):
    filename = search_Term
    url = 'https://duckduckgo.com/html?q='+search_Term
    r = requests.get(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'})

        rows = []
        if link and title:
            link = link['href']
            title = title.get_text()
            if description:
                description = description.get_text()
                rows.append({'title':title.strip(), 'link':link.strip(), 'description':description.strip()})
                # print(title.strip(), link.strip())
                # print(description.strip())
                # print('*'* 80)

        with open(filename+'.csv', 'a', encoding='utf-8',newline='') as csv_file:
            file_is_empty = os.stat(filename+'.csv').st_size==0
            fieldname = ['title','link','description']
            writer = csv.DictWriter(csv_file,fieldnames=fieldname)
            if file_is_empty:
                writer.writeheader()
            writer.writerows(rows)

scrape('tree')

这会创建tree.csv. 在 LibreOffice 中,它看起来像这样:

在此处输入图像描述

于 2019-07-27T07:42:11.053 回答
0

您可以使用适当的有效负载发出 post http 请求,以获取所需的内容并将它们写入 csv 文件。我使用 python 作为搜索关键字,这就是它产生的结果:

import csv
import requests
from bs4 import BeautifulSoup

URL = "https://duckduckgo.com/html/"

payload = {
    'q': 'python',
    'b': '',
    'kl': 'us-en'
}

r = requests.post(URL,data=payload,headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(r.text,"lxml")

with open("output.csv","w",newline="",encoding="UTF-8") as infile:
    writer = csv.writer(infile)
    for item in soup.select(".result__body"):
        title = item.select_one(".result__a").text
        link = item.select_one(".result__a").get("href")
        desc = item.select_one(".result__snippet").text
        desc_link = item.select_one(".result__snippet").get("href")
        print(f'{title}\n{link}\n{desc}\n{desc_link}\n')
        writer.writerow([title,link,desc,desc_link])

结果如下:

Welcome to Python.org
https://www.python.org/
The official home of the Python Programming Language. Compound Data Types. Lists (known as arrays in other languages) are one of the compound data types that Python understands.
https://www.python.org/

Python (programming language) - Wikipedia
https://en.wikipedia.org/wiki/Python_%28programming_language%29
Python is an interpreted, high-level, general-purpose programming language.Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace.Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
https://en.wikipedia.org/wiki/Python_%28programming_language%29

Python Tutorial - w3schools.com
https://www.w3schools.com/python/
Python is a programming language. Python can be used on a server to create web applications. Start learning Python now »
https://www.w3schools.com/python/
于 2019-07-27T07:45:05.527 回答