【问题标题】:How to extract data from multiple URL using python如何使用python从多个URL中提取数据
【发布时间】:2017-09-04 10:32:47
【问题描述】:

您好,我想从多个 URL 中删除数据,我正在这样做

for i in range(493):
    my_url = 'http://tis.nhai.gov.in/TollInformation?TollPlazaID={}'.format(i)

但它没有给我完整的数据,它只打印最后一个 url 数据,

这是我的代码,请帮助

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import psycopg2
import operator


for i in range(493):
    my_url = 'http://tis.nhai.gov.in/TollInformation?TollPlazaID={}'.format(i)

    uClient = uReq(my_url)
    page1_html = uClient.read()
    uClient.close()
    # html parsing
    page1_soup = soup(page1_html, 'html.parser')

    # grabing data
    containers = page1_soup.findAll('div', {'class': 'PA15'})

    # Make the connection to PostgreSQL
    conn = psycopg2.connect(database='--',user='--', password='--', port=--)
    cursor = conn.cursor()
    for container in containers:
        toll_name1 = container.p.b.text
        toll_name = toll_name1.split(" ")[1]

        search1 = container.findAll('b')
        highway_number = search1[1].text.split(" ")[0]

        text = search1[1].get_text()
        onset = text.index('in')
        offset = text.index('Stretch')
        state = str(text[onset +2:offset]).strip(' ')

        location = list(container.p.descendants)[10]
        mystr = my_url[my_url.find('?'):]
        TID = mystr.strip('?TollPlazaID=')

        query = "INSERT INTO tollmaster (TID, toll_name, location, highway_number, state) VALUES (%s, %s, %s, %s, %s);"
        data = (TID, toll_name, location, highway_number, state)

        cursor.execute(query, data)

# Commit the transaction
conn.commit()

但它只显示倒数第二个网址数据

【问题讨论】:

  • 你的“格式”语句只生成一个 url...
  • 但是我还有很多其他的网址,例如http://tis.nhai.gov.in/TollInformation?TollPlazaID=203 http://tis.nhai.gov.in/TollInformation?TollPlazaID=258 ,那我该怎么办?
  • 我想像:my_url = 'http://tis.nhai.gov.in/TollInformation?TollPlazaID={}'.format(i)
  • 仍然抛出错误` tbody = soup('table', {"class": "tollinfotbl"})[0].find_all('tr')[1:] IndexError: list index out范围`
  • 检查错误提示您正在尝试访问不存在的项目

标签: python web-scraping beautifulsoup


【解决方案1】:

好像有些页面缺少你的关键信息,你可以使用error-catching,像这样:

try: 
    tbody = soup('table', {"class": "tollinfotbl"})[0].find_all('tr')[1:]
except IndexError:
    continue  # Skip this page if no items were scrapped

您可能想要添加一些日志记录/打印信息来跟踪不存在的表。

编辑: 它仅显示最后一页的信息,因为您在for 循环之外提交事务,为每个i 覆盖您的conn。只需将conn.commit() 放入for 循环的远端即可。

【讨论】:

  • 嘿抱歉,那是错误的代码,请看看我的更新代码,这里只有最后一个 url 数据在表格中打印
猜你喜欢
  • 1970-01-01
  • 2022-09-30
  • 2014-10-01
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
相关资源
最近更新 更多