【问题标题】:Unable to use multiple attributes within .select() method无法在 .select() 方法中使用多个属性
【发布时间】:2019-05-30 19:27:38
【问题描述】:

我在 python 中编写了一个脚本来获取网站上不同帖子的不同链接。如果我选择像a[href^='/questions/'].question-hyperlink 这样的任何单个属性,我可以获得所需的输出。

我的问题是:我如何在.select() 方法中使用来自同一节点的两个属性,就像我在下面尝试过的那样?

到目前为止我的尝试(没有结果;也没有错误):

import requests
from bs4 import BeautifulSoup

link = "https://stackoverflow.com/questions/tagged/web-scraping"

res = requests.get(link)
soup = BeautifulSoup(res.text,'lxml')
for item in soup.select("a[href^='/questions/'].question-hyperlink"):
    print(item.get("href"))

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup css-selectors


    【解决方案1】:

    实际上,您的代码运行良好。我得到了你的预期结果。我只需要 pip install lxml 和 BeautifulSoup4 来运行它。

    /questions/54035304/unable-to-use-multiple-attributes-within-select-method
    /questions/54035268/div-returns-empty-while-scraping-price-using-beautifulsoup-everything-else-lik
    /questions/54035199/scrapy-restart-scrapoxy-instances
    /questions/54034484/how-i-can-read-multiple-web-addresses-with-sign-in-address-that-block-dynami
    

    附:您需要使用最新的lxmlbs4 版本。所以你可能需要执行

    • pip3 install lxml --upgrade
    • pip3 install beautifulsoup4 --upgrade

    【讨论】:

    • 您能分享一下您是如何定义您的BeautifulSoup 实例的吗?
    • 我没有对@robots.txt 代码进行任何更改。 import requests from bs4 import BeautifulSoup link = "https://stackoverflow.com/questions/tagged/web-scraping" res = requests.get(link) soup = BeautifulSoup(res.text,'lxml') for item in soup.select("a[href^='/questions/'].question-hyperlink"): print(item.get("href"))
    • AFAIK,是的,您需要安装lxml 才能使用lxml 解析器,但即使安装了lxml,我仍然看不到任何结果......你能分享bs4,lxml吗, Python 版本?
    • Python 3.6、bs4 4.7.0、lxml 4.2.6
    • 试试上面我用过的版本。我不确定是什么阻止您的代码产生结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    相关资源
    最近更新 更多