当我向这个 url 发出 get 请求时:http : //www.waterwaysguide.org.au/waterwaysguide/access-point/4980/partial使用浏览器返回一个完整的 html 页面。但是,当我使用 python requests 模块发出 GET 请求时,只返回了 html 的一部分,并且缺少核心内容。
如何更改我的代码以便我可以获得丢失的数据?
这是我正在使用的代码;
import requests
def get_data(point_num):
base_url = 'http://www.waterwaysguide.org.au/waterwaysguide/access-point/{}/partial'
r = requests.get(base_url)
html_content = r.text
print(html_content)
get_data(4980)
Run Code Online (Sandbox Code Playgroud)
运行代码的结果如下所示。里面内容DIV CLASS =“查看浏览水路访问点页...缺失。
import requests
def get_data(point_num):
base_url = 'http://www.waterwaysguide.org.au/waterwaysguide/access-point/{}/partial'
r = requests.get(base_url)
html_content = r.text
print(html_content)
get_data(4980)
Run Code Online (Sandbox Code Playgroud)
我目前正在尝试使用Python 3.6中的请求和BeautifulSoup模块进行练习,并遇到了一个我似乎无法在其他问题和答案中找到任何信息的问题.
似乎在页面的某个时刻,Beuatiful Soup停止识别标签和ID.我试图从这样的页面中提取播放数据:
http://www.pro-football-reference.com/boxscores/201609080den.htm
import requests, bs4
source_url = 'http://www.pro-football-reference.com/boxscores/201609080den.htm'
res = requests.get(source_url)
if '404' in res.url:
raise Exception('No data found for this link: '+source_url)
soup = bs4.BeautifulSoup(res.text,'html.parser')
#this works
all_pbp = soup.findAll('div', {'id' : 'all_pbp'})
print(len(all_pbp))
#this doesn't
table = soup.findAll('table', {'id' : 'pbp'})
print(len(table))
Run Code Online (Sandbox Code Playgroud)
在Chrome中使用检查器,我可以看到该表肯定存在.我也尝试在HTML的后半部分使用'div'和'tr',它似乎不起作用.我已经尝试了标准的'html.parser'以及lxml和html5lib,但似乎没有任何效果.
我在这里做错了什么,或者HTML或其格式中是否存在阻止BeautifulSoup正确查找以后标签的内容?我遇到过这家公司(hockey-reference.com,basketball-reference.com)运营的类似网页的问题,但是能够在其他网站上正确使用这些工具.
如果它是HTML的东西,有没有更好的工具/库来帮助提取这些信息?
BF,谢谢你的帮助
我正在使用 requests-html 并尝试渲染功能,但收效甚微。当我使用 python3.8 运行这个脚本时
#!/usr/bin/python3
from requests_html import HTML
file = "scrape/temp_file2.html"
with open(file) as html_file:
source = html_file.read()
html = HTML(html=source)
html.render()
match = html.find('#footer', first=True)
try:
print(match.hmtl)
except:
print('not found')
Run Code Online (Sandbox Code Playgroud)
它会导致回溯:
python3 scrape/test1.py
Traceback (most recent call last):
File "scrape/test1.py", line 10, in <module>
html.render()
File "/home/pi/.local/lib/python3.8/site-packages/requests_html.py", line 586, in render
self.browser = self.session.browser # Automatically create a event loop and browser
File "/home/pi/.local/lib/python3.8/site-packages/requests_html.py", line 730, in browser
self._browser = self.loop.run_until_complete(super().browser)
File "/usr/local/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete …Run Code Online (Sandbox Code Playgroud) 我正在开展一个项目,我需要抓取几个网站并从中收集不同类型的信息.文本,链接,图像等信息
我正在使用Python.我在HTML页面上为此目的尝试了BeautifulSoup并且它可以工作,但是在解析包含大量JavaScript的网站时我很困难,因为这些文件的大部分信息都存储在<script>标记中.
任何想法如何做到这一点?
我正在尝试构建一个简短的 Python 程序,用于提取 Pewdiepie 的订阅者数量,该程序在 Socialblade 上每秒更新一次,以在终端中显示。我想要每 30 秒一次的数据。
我尝试过使用 PyQt,但它很慢,我转向 dryscrape,稍微快一点,但也没有按照我想要的方式工作。我刚刚找到 Invader 并编写了一些简短的代码,但仍然存在同样的问题:返回的数字是执行页面上的 Javascript 之前的数字:
from invader import Invader
url = 'https://socialblade.com/youtube/user/pewdiepie/realtime'
invader = Invader(url, js=True)
subscribers = invader.take(['#rawCount', 'text'])
print(subscribers.text)
Run Code Online (Sandbox Code Playgroud)
我知道这些数据可以通过网站的 API访问,但它并不总是有效,有时它只是重定向到this。
有没有办法在页面上的Javascript修改计数器之后而不是之前获取这个数字?哪种方法对您来说最好?提取它:
感谢您的建议!
我正在尝试以编程方式从 JavaScript 中的https://rarity.tools/upcoming/页面访问表中的数据。由于该网站通过 JavaScript 加载,因此我一直在使用 puppeteer。该网站有多个表(总共 4 个),我希望能够引用每个表并检查它们有多少行。
我最初尝试使用 nth-of-type,但我尝试从中接收数据的网站似乎没有以允许我使用 nth-of-type 或 nth-child 的方式构建其页面(请请参阅:在 javascript 错误中使用 pupeteer 访问第 n 个表并计算行数:“无法找到与选择器“table:nth-of-type(2) > tr”匹配的元素”” )。
相反,我尝试创建一个 for 循环来将每个表的innerHTML 设置为其自己的变量,然后根据索引分析HTML 字符串。如果我对数字进行硬编码,以下内容将返回正确的值:
console.log(table_html)
let table_html = await page.evaluate(
() => document.querySelectorAll('table')[2].innerHTML
)
Run Code Online (Sandbox Code Playgroud)
但是,一旦我将其设置为循环:
for (let j = 0; j < numTables; j++) {
let table_html = await page.evaluate(
(j) => document.querySelectorAll('table')[j].innerHTML
)
console.log(table_html)
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
错误:评估失败:TypeError:无法在ExecutionContext._evaluateInternal 处的puppeteer_evaluation_script :1:46 处读取未定义的属性(读取“innerHTML”)(C:\Users\kylel\Desktop\NFTSorter_IsolatedJS\node_modules\puppeteer\lib\cjs
puppeteer\common \ExecutionContext.js:221:19) 在 processTicksAndRejections (internal/process/task_queues.js:95:5) 在异步 ExecutionContext.evaluate (C:\Users\kylel\Desktop\NFTSorter_IsolatedJS\node_modules\puppeteer\lib\cjs\pup peteer\common\ExecutionContext.js:110:16) 在异步获取 (C:\Users\kylel\Desktop\NFTSorter_IsolatedJS\app.js:35:30)
所有代码: …
我尝试使用下面的代码在此网页(https://www.meleenumerique.com/scientist_comite)上抓取人员的名字+姓氏,但它不起作用。我怎样才能确定它出了什么问题?
这是我写的代码
from lxml import html
import csv,os,json
import requests
url="https://www.meleenumerique.com/scientist_comite"
r=requests.get(url)
t=html.fromstring(r.content)
title=t.xpath('/html/head/title/text()')
#Create the list of speaker
speaker=t.xpath('//span[contains(@class,"speaker-name")]//text()')
print(title)
print("Speakers:",speaker)
Run Code Online (Sandbox Code Playgroud) 我正在使用 bs4进行网页抓取天气搜索 Google,而 Python<span>在有标签时找不到标签。我怎么解决这个问题?
我试图找到这<span>与class 和的id,但都失败了。
<div id="wob_dcp">
<span class="vk_gy vk_sh" id="wob_dc">Clear with periodic clouds</span>
</div>
Run Code Online (Sandbox Code Playgroud)
以上是我试图在页面中抓取的 HTML 代码:
response = requests.get('https://www.google.com/search?hl=ja&ei=coGHXPWEIouUr7wPo9ixoAg&q=%EC%9D%BC%EB%B3%B8+%E6%A1%9C%E5%B7%9D%E5%B8%82%E7%9C%9F%E5%A3%81%E7%94%BA%E5%8F%A4%E5%9F%8E+%EB%82%B4%EC%9D%BC+%EB%82%A0%EC%94%A8&oq=%EC%9D%BC%EB%B3%B8+%E6%A1%9C%E5%B7%9D%E5%B8%82%E7%9C%9F%E5%A3%81%E7%94%BA%E5%8F%A4%E5%9F%8E+%EB%82%B4%EC%9D%BC+%EB%82%A0%EC%94%A8&gs_l=psy-ab.3...232674.234409..234575...0.0..0.251.929.0j6j1......0....1..gws-wiz.......35i39.yu0YE6lnCms')
soup = BeautifulSoup(response.content, 'html.parser')
tomorrow_weather = soup.find('span', {'id': 'wob_dc'}).text
Run Code Online (Sandbox Code Playgroud)
但是这个代码失败了,错误是:
Traceback (most recent call last):
File "C:\Users\sungn_000\Desktop\weather.py", line 23, in <module>
tomorrow_weather = soup.find('span', {'id': 'wob_dc'}).text
AttributeError: 'NoneType' object has no attribute 'text'
Run Code Online (Sandbox Code Playgroud)
请解决这个错误。
我想使用 python 脚本废弃由 javascript 函数创建的 DIV 内容。我已经尝试过使用 BS4 并且通过这样做我无法获得动态数据。相反,它只显示源代码。
示例代码:
import requests
from bs4 import BeautifulSoup
URL = "https://rawgit.com/skysoft999/tableauJS/master/example.html"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
for row in soup.findAll('div', attrs = {'class':'quote'}):
print(row)
print(soup.prettify())
Run Code Online (Sandbox Code Playgroud)
示例 HTML 源代码位于Pastebin 中
要提取的样本数据:
我目前有以下格式的JSON。一些键值的格式不正确,因为它们缺少双引号(“)
如何修复这些键值以使其带有双引号?
{
Name: "test",
Address: "xyz",
"Age": 40,
"Info": "test"
}
Run Code Online (Sandbox Code Playgroud)
需要:
{
"Name": "test",
"Address": "xyz",
"Age": 40,
"Info": "test"
}
Run Code Online (Sandbox Code Playgroud)
使用下面的文章,我能够在上面的INVALID JSON中找到这样的键值。但是,我找不到用双引号替换这些找到的值的有效方法。
s = "Example: String"
out = re.findall(r'\w+:', s)
Run Code Online (Sandbox Code Playgroud)
我打算从 pyqt QWebEngine 执行一个 javascript 函数。我遵循了一个使用地图的示例,并在按下 Qt 应用程序按钮时检索了地图绑定,并编写了一个小示例。
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
function helloWorld(param1, param2) {
return "Hello world " + param1 + " " + param2;
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Python:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.form_widget = FormWidget(self)
_widget = QWidget()
_layout = QVBoxLayout(_widget)
_layout.addWidget(self.form_widget)
self.setCentralWidget(_widget)
class FormWidget(QWidget):
def __init__(self, parent):
super(FormWidget, self).__init__(parent)
self.__controls()
self.__layout()
self.browser.page().runJavaScript("helloWorld()", self.ready) …Run Code Online (Sandbox Code Playgroud) python ×10
web-scraping ×7
javascript ×4
python-3.x ×2
arrays ×1
html ×1
json ×1
loops ×1
lxml ×1
parsing ×1
puppeteer ×1
pyqt ×1
pyqt5 ×1
querying ×1
regex ×1
web-crawler ×1