【问题标题】:Data not properly being scraped from a given website using python未使用 python 从给定网站正确抓取数据
【发布时间】:2015-06-30 02:03:13
【问题描述】:

我正在尝试从“thegolfcourse.net”网站提取高尔夫球场信息。我的目标是从网站上收集美国 18000 多个高尔夫球场的名称、地址和电话号码。我运行了我的脚本,但它没有从网站生成所有数据。有 18000 多个高尔夫球场,但我只从网站下载了大约 200 多个网站。我不知道我的循环是否错误,或者我没有根据我的代码访问所有数据,而且我的数据中有空格,我想知道如何正确提取数据。

这是我的脚本:

import csv
import requests 
from bs4 import BeautifulSoup

courses_list = []

for i in range(56):
 url="http://www.thegolfcourses.net/page/{}?ls&location&orderby=title".format(i)
 r = requests.get(url)
 soup = BeautifulSoup(r.content)


g_data2=soup.find_all("article")


for item in g_data2:
  try:
    name = item.contents[5].find_all("a")[0].text
    print name
  except:
        name=''      
  try:
    phone= item.contents[13].find_all("p",{"class":"listing-phone"})[0].text
  except:
      phone=''
  try:
    address= item.contents[13].find_all("p",{"class":"listing-address"})[0].text
  except:
      address=''

  course=[name,phone,address]
  courses_list.append(course)


with open ('PGN.csv','a') as file:
  writer=csv.writer(file)
  for row in courses_list:
          writer.writerow([s.encode("utf-8") for s in row])

【问题讨论】:

  • 您想要的输出是什么样的?编辑您的问题,并举例说明您希望它的外观。
  • 乍一看,您似乎在抓取前 56 页,但我在网站上看到了 1,907 页。就额外空格而言,您可能会得到前导和尾随空格。在字符串上使用.strip() 来清理它。
  • 如何获取所有 1907 页?
  • @Gonzalo68 查看我提供的关于如何获取所需内容的答案。

标签: python csv beautifulsoup scrape


【解决方案1】:

首先,您的代码没有抓取所有内容,因为您将范围设置为 56。这对于测试来说很好,但如果您想抓取您需要设置的所有内容

for i in range(1907):

这会转到 1907,因为由于将 .format(i+1) 添加到 URL 部分,它将在 1907 停止。

此外,您的for 循环中有几个错误。这些可能是您在 StackOverflow 上发布时出现的问题,但我还是清理了它们。

当我第一次运行您的代码时,我看到了“间距”是什么。当您解析 HTML 时,您解析它以寻找 article 标记,但该标记还负责在示例链接中显示 "Listings found for "" near "" 的第一个搜索结果。您可以使用我在这里所做的事情来缩小网络抓取时的范围:

g_data2 = soup.find_all("article",{"itemtype":"http://schema.org/Organization"})

这将使您更轻松地进行抓取,只需抓取包含articleitemtype = http://schema.org/Organization" 的标签中的数据。这足够独特,幸运的是所有条目都匹配该格式。

我还将您的 csvwritera 更改为 wb,每次运行脚本时都会启动一个新的 CSV,并且不会附加到它。

这是最终脚本:

import csv
import requests
from bs4 import BeautifulSoup

courses_list = []

for i in range(1907):
    url="http://www.thegolfcourses.net/page/{}?ls&location&orderby=title".format(i+1)
    r = requests.get(url)
    soup = BeautifulSoup(r.text)
    #print soup
    g_data2 = soup.find_all("article",{"itemtype":"http://schema.org/Organization"})

    for item in g_data2:
        try:
            name = item.find_all("h2",{'class':'entry-title'})[0].text
            print name
        except:
            name=''
            print "No Name found!"
        try:
            phone= item.find_all("p",{"class":"listing-phone"})[0].text
        except:
            phone=''
            print "No Phone found!"
        try:
            address= item.find_all("p",{"class":"listing-address"})[0].text
        except:
            address=''
            print "No Address found!"
        course=[name,phone,address]
        courses_list.append(course)

with open ('PGN.csv','wb') as file:
    writer=csv.writer(file)
    for row in courses_list:
        writer.writerow([s.encode("utf-8") for s in row])

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多