【发布时间】:2019-07-31 10:07:09
【问题描述】:
我有一个包含多个输入字段的 Intranet 页面,我需要 Scrapy 使用网页“搜索产品”输入字段运行搜索,它的 id 为“searchBox”
我已经能够使用 Scrapy 和 Beautiful Soup 锁定正确的搜索框,但我不确定如何正确地将这些数据传递回 Scrapys 表单提交功能。
在方法 1 中,我尝试简单地将结果作为输入传递给 Scrapys FormRequest.from_response 函数,但它不起作用。
方法一 - 使用 Scrapy 查找数据
#Search for products
def parse(self, response):
##Let's try search using scrapy only
sel = Selector(response)
results = sel.xpath("//*[contains(@id, 'searchBox')]")
for result in results:
print (result.extract()) #Print out what scrapy found
return scrapy.FormRequest.from_response(results, formdata = {'Item': 'Whirlpool Washing Machine'}) #formdata is the data we are sending
方法二——用Beautiful soup查找数据
#Search for products
def parse(self, response):
##Let's try search using Beautiful Soup only
soup = BeautifulSoup(response.text, 'html.parser')
product_search = []
product_search.append(soup.find("input", id="searchBox"))
print(product_search) #Print what BS found
【问题讨论】:
标签: python beautifulsoup scrapy