嗨,我对Python和Web抓取比较陌生。我正在尝试从该页(https://www.jmesales.com/kuriyama-3-4-in-brass-quick-couplings/)下拉菜单中的每个产品选项中提取数据。我相信页面不使用JavaScript,我更愿意使用请求和BeautifulSoup,而不是webdriver。我有代码可以获取每个选项的名称和属性值,但我不确定如何访问与每个选项关联的定价和规格数据。这是我的代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
url = 'https://www.jmesales.com/kuriyama-3-4-in-brass-quick-couplings/'
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text,'lxml')
options = [item['value'] for item in soup.select('#attribute_select_42800 option')]
for option in options:
print(option)
我想访问每个选项的价格和相关数据。任何帮助都将不胜感激
希望这将有助于:
尝试类似的方法:
印刷品:
以上代码仅以您拥有的特定url为例,可以解析多个url:
这不是你一直在寻找的答案,但对于网络垃圾,我建议使用硒
https://selenium-python.readthedocs.io/
只要打开浏览器,你就可以做任何你想做的事情。我要做的是查找XPath并查找要迭代的模式
相关问题 更多 >
编程相关推荐