【问题标题】:How to scrape multiple websites with different data in urls如何在 url 中抓取具有不同数据的多个网站
【发布时间】:2020-03-02 15:04:24
【问题描述】:

我正在从一个网页中抓取一些数据,其中网址末尾有产品的 id,它似乎重写了每一行的数据,就像它没有从下一行附加数据一样,我不'不知道到底发生了什么,如果我的第一个 for 是错误的,或者缩进,我之前尝试过没有字典,它是附加的,但在同一行,我转置它,但没有按我想要的那样工作,所以我做了这样,现在它不会附加下一行,请帮助

data_cols = []
cols = {'pro_header': [],
        'pro_id': [],
        .
        .
        .
        'pro_uns5': []
        }
#the id for each product
fileID = open('idProductsList.txt', 'r')
proIDS = fileID.read().split()
for proID in proIDS:
    url = 'https:/website.com/mall/es/mx/Catalog/Product/' + proID
    html = urllib2.urlopen(url).read()
    soup = bs.BeautifulSoup(html , 'lxml')
    table = soup.find("table",{"class": "ProductDetailsTable"})
    rows = table.find_all('tr')
    for row in rows:
        labels.append(str(row.find_all('td')[0].text))
        try:
            data.append(str(row.find_all('td')[1].text))
        except IndexError:
            data.append('')

    cols['pro_header'].append(data[0])
    cols['pro_id'].append(data[1])
    .
    .
    .
    cols['pro_uns5'].append(data[43])
    df = pd.DataFrame(cols)
    df.set_index
    #df.reindex()
    df.to_csv('sample1.csv')

实际输出为:

pro_id  pro_priceCostumer   pro_priceData
1FK7011-5AK24-1AA3  " Mostrar precios
"   PM300:Producto activo
1FK7011-5AK24-1AA3  " Mostrar precios
"   PM300:Producto activo
1FK7011-5AK24-1AA3  " Mostrar precios
"   PM300:Producto activo

应该是这样的(这只是数据的一个小表示)

pro_id  pro_priceCostumer   pro_priceData
1FK7011-5AK24-1AA3  " Mostrar precios
"   PM300:Producto activo
1FK7011-5AK24-1JA3  " Mostrar precios
"   PM300:Producto activo
1FK7022-5AK21-1UA0  " Mostrar precios
"   PM300:Producto activo

【问题讨论】:

  • 可以分享一下网址吗?我认为访问数据 API 可能更快/更容易......或者因为你正在抓取 <table> 标签,所以只需使用 pandas 来拉它。
  • 我不确定您的实际输出和所需输出之间有什么区别。它们看起来一样

标签: python python-2.7 web-scraping beautifulsoup scrapinghub


【解决方案1】:

我猜标签是作为一个变量工作的。要附加此内容,您需要使用列表。 在代码顶部添加labels=list() 作为全局变量。 data 也应该这样做。

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 2021-04-06
    • 2023-01-05
    • 2015-01-15
    • 1970-01-01
    • 2018-06-09
    • 2020-12-29
    • 2020-12-19
    相关资源
    最近更新 更多