【问题标题】:Web scraping with open API on multiple keywords使用开放 API 对多个关键字进行 Web 抓取
【发布时间】:2021-09-19 21:36:25
【问题描述】:

我对 Python 还很陌生,几天前才开始涉足它。 我试图尽我所能进行故障排除,但我似乎无法让它以我想要的方式工作。 我尝试了以下方法:df.iterrowsdf.ilocdf.at、 和df.iat

当我尝试将索引输入为行时出现的错误主要是keyerror。 当我输入一个整数作为行时,它可以工作,但仅适用于 1 个关键字(因为它的值是用索引整数指定的)。 我希望代码能够针对每个关键字运行和循环。 例子: 如果我有 40 个关键字,我想要 4000 行的最终结果。 如果我有 100 个关键字,我想要 10000 行的最终结果。

如果我的代码杂乱无章且丑陋,我深表歉意。

这是我的项目详情。 目标:抓取具有多个关键字的网站以获取产品数据

方法:使用网站开放的API提取数据。

流程:关键字数据框 > 将第一个关键字输入 API 并提取数据 > 打印 100 行搜索结果 > 重复第 2、3、4、...关键字

我希望有人可以帮助我。

谢谢。

import requests

# Data manipulation
import pandas as pd
import numpy as np

# Create sample keyword dataframe from dict
data3 = {'part number': [123456, 234567, 345678, 456789], 'search keyword': ['apple', 'banana', 'orange', 'mango']}

df3 = pd.DataFrame.from_dict(data3)
print(df3)

# Input API parameters
for i in df3['search keyword']:
    Shopee_url = 'https://shopee.com.my'
    keyword_search = df3.loc[i, 'Search Keyword']
    headers = {
     'User-Agent': 'Chrome',
     'Referer': '{}search?keyword={}'.format(Shopee_url, keyword_search)
    }
    url = 'https://shopee.com.my/api/v2/search_items/?by=relevancy&keyword={}&limit=100&newest=0&order=desc&page_type=search'.format(keyword_search)

    # Shopee API request
    r = requests.get(url, headers = headers).json()

    # Shopee scraping script
    title_list = []
    min_price_after_discount_list = []
    standard_retail_price_list = []
    shop_location_list = []
    shop_id_list = []
    item_id_list = []
    for item in r['items']:
        title_list.append(item['name'])
        min_price_after_discount_list.append(item['price_min'])
        standard_retail_price_list.append(item['price_min_before_discount'])
        shop_location_list.append(item['shop_location'])
        shop_id_list.append(item['shopid'])
        item_id_list.append(item['itemid'])

# Define a dictionary from web scraped data
data2 = {'Part Number': "123456",
        'Product Name': title_list,
        'Min Price After Discount': min_price_after_discount_list,
        'Extracted Standard Retail Price': standard_retail_price_list,
        'Seller Location': shop_location_list,
        'Seller ID': shop_id_list,
        'Product ID': item_id_list,
        'Product URL': "https://shopee.com.my/product/shopid/itemid/"
       }

# Convert the dictionary into DataFrame
df2 = pd.DataFrame(data2)

【问题讨论】:

  • 您希望keyword_search 包含单词本身还是零件编号?
  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
  • 首先您应该使用print() 来查看变量中的内容。然后你就不需要问问题了。
  • @ezzeddin 我想要关键字搜索来拥有单词本身。零件号只是作为附加列存在。
  • @furas 没有错误消息。但是 data2 的打印数据框并不符合我的预期。

标签: python pandas web-scraping data-science


【解决方案1】:

如果您在每次迭代中只需要搜索词,则不需要 .loc,因为您已经使用迭代器 i 遍历搜索词

keyword_search = i

【讨论】:

  • 我也试过了。但它只调用搜索词的最后一行。至少,我的打印结果是这样,打印了 99 行。
  • @bluemango26 你只得到最后一行,因为你在代码中有其他错误 - 你在 for-loop 中创建列表,所以在每个循环中它都会创建新的空列表并删除以前的内容。您应该在for-loop 之前创建列表。或者您应该在每个循环中创建 data2 并将此 data2 添加到包含所有数据的列表中。
  • @furas 非常感谢您的帮助!对我来说真的是一个错误。我已经修复了代码,它现在可以正常工作了!
猜你喜欢
  • 2023-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2021-08-28
相关资源
最近更新 更多