我正在研究Web抓取项目,我是python的新手,但是我对如何插入/获取用户url输入表单并将其插入python脚本以抓取此输入url并将数据显示回html文件
我尝试了很多天,但是还是没有。
这是代码(html):
<form method="get" onsubmit="return formsubmit();" action="test2.py">
<input type="url" name="asin" id="asin" class="input-text" placeholder="Enter your link here e.g. (https://www.amazon.com/gp/product/B064GWQXUJ1/)">
</form>
test.py:
import requests
from bs4 import BeautifulSoup
import re
URL = "https://www.amazon.com/"
# This URL I need it dynamically as per user input in the html file
res = requests.get(URL, headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(res.text, "lxml")
price = soup.select_one("#priceblock_ourprice").text
size = soup.select_one("#dropdown_selected_size_name .a-dropdown-prompt").text
color = soup.select_one("#dropdown_selected_color_name .a-dropdown-prompt").text
shipping_weight = soup.select_one("#productDetails_detailBullets_sections1 tr:nth-child(3) td").text
title = soup.select_one("#productTitle").text.split()
# I need to display the results into the html file with all these details
print(price)
print(size)
print(color)
print(shipping_weight)
print(title)
请帮忙吗?