python – BeautifulSoup解析器无法访问html元素
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – BeautifulSoup解析器无法访问html元素,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3449字,纯文字阅读大概需要5分钟。
内容图文
![python – BeautifulSoup解析器无法访问html元素](/upload/InfoBanner/zyjiaocheng/703/caaea216fe6347aba7e89c93d971353c.jpg)
我试图刮掉所有列表的hrefs.我对beautifulsoup相当新,之前做过一些刮痧,但之前做过一些刮痧.但我不能为我的生活提取.见下面我的代码.运行此脚本时,容器的长度为零.
我也尝试选择价格(soup.findAll(“span”,{“class”:“amount”}),但它没有反映.欢迎任何建议:)
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
url = 'https://www.takealot.com/computers/laptops-10130'
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
req = urllib.request.Request(url, headers=headers)
resp = urllib.request.urlopen(req)
respData = str(resp.read())
soup = BeautifulSoup(respData, 'html.parser')
container = soup.find_all("div", {"class": "p-data left"})
解决方法:
该页面使用JavaScript呈现.有几种方法可以渲染和刮擦它.
我可以用Selenium刮它.
首先安装Selenium:
sudo pip3 install selenium
然后获得一个驱动程序https://sites.google.com/a/chromium.org/chromedriver/downloads,你可以使用无头版Chrome“Chrome Canary”,如果你在Windows或Mac上.
from bs4 import BeautifulSoup
from selenium import webdriver
browser = webdriver.Chrome()
url = ('https://www.takealot.com/computers/laptops-10130')
browser.get(url)
respData = browser.page_source
browser.quit()
soup = BeautifulSoup(respData, 'html.parser')
containers = soup.find_all("div", {"class": "p-data left"})
for container in containers:
print(container.text)
print(container.find("span", {"class": "amount"}).text)
或者使用PyQt5
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWebKit import *
from PyQt5.QtWebKitWidgets import QWebPage
from PyQt5.QtWidgets import QApplication
from bs4 import BeautifulSoup
import sys
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
url = 'https://www.takealot.com/computers/laptops-10130'
r = Render(url)
respData = r.frame.toHtml()
soup = BeautifulSoup(respData, 'html.parser')
containers = soup.find_all("div", {"class": "p-data left"})
for container in containers:
print (container.text)
print (container.find("span", {"class":"amount"}).text)
或者使用dryscrape:
from bs4 import BeautifulSoup
import dryscrape
url = 'https://www.takealot.com/computers/laptops-10130'
session = dryscrape.Session()
session.visit(url)
respData = session.body()
soup = BeautifulSoup(respData, 'html.parser')
containers = soup.find_all("div", {"class": "p-data left"})
for container in containers:
print(container.text)
print(container.find("span", {"class": "amount"}).text)
所有情况下的产出:
Dell Inspiron 3162 Intel Celeron 11.6" Wifi Notebook (Various Colours)11.6 Inch Display; Wifi Only (Red; White & Blue Available)R 3,999R 4,999i20% OffeB 39,990Discovery Miles 39,990On Credit: R 372 / monthi
3,999
HP 250 G5 Celeron N3060 Notebook - Dark ash silverNBHPW4M70EAR 4,499R 4,999ieB 44,990Discovery Miles 44,990On Credit: R 419 / monthiIn StockShippingThis item is in stock in our CPT warehouse and can be shipped from there. You can also collect it yourself from our warehouse during the week or over weekends.CPT?|?ShippingThis item is in stock in our JHB warehouse and can be shipped from there. No collection facilities available, sorry!JHBWhen do I get it?
4,499
Asus Vivobook ...
但是,在使用您的URL进行测试时,我发现每次都无法重现结果,有时在页面呈现后我没有在“容器”中获得任何内容.
内容总结
以上是互联网集市为您收集整理的python – BeautifulSoup解析器无法访问html元素全部内容,希望文章能够帮你解决python – BeautifulSoup解析器无法访问html元素所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。