我知道它requests.get()提供了一个HTTP接口,以便程序员可以向HTTP服务器发出各种请求.
这告诉我某个端口必须打开,以便请求可以发生.
考虑到这一点,如果脚本被停止(例如,通过键盘中断,那么正在执行脚本的机器仍然连接到互联网),在请求被应答/完成之前会发生什么?
端口/连接是否仍然打开?
端口/连接是否自动关闭?
我在python中编写了一个脚本,以便从javascript呈现的网页中获取最后一笔交易的价格.我可以得到内容如果我选择去selenium.我的目标是不使用任何类似的浏览器模拟器,selenium因为最新版本的Requests-HTML应该具有解析javascript加密内容的能力.但是,我无法顺利完成任务.当我运行脚本时,我收到以下错误.任何有关这方面的帮助将受到高度赞赏.
网站地址:webpage_link
我尝试过的脚本:
import requests_html
with requests_html.HTMLSession() as session:
r = session.get('https://www.gdax.com/trade/LTC-EUR')
js = r.html.render()
item = js.find('.MarketInfo_market-num_1lAXs',first=True).text
print(item)
Run Code Online (Sandbox Code Playgroud)
这是完整的追溯:
Exception in callback NavigatorWatcher.waitForNavigation.<locals>.watchdog_cb(<Task finishe...> result=None>) at C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py:49
handle: <Handle NavigatorWatcher.waitForNavigation.<locals>.watchdog_cb(<Task finishe...> result=None>) at C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py:49>
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py", line 145, in _run
self._callback(*self._args)
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py", line 52, in watchdog_cb
self._timeout)
File "C:\Users\ar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyppeteer\navigator_watcher.py", line 40, in _raise_error
raise error
concurrent.futures._base.TimeoutError: Navigation Timeout Exceeded: 3000 ms exceeded
Traceback (most recent call …Run Code Online (Sandbox Code Playgroud) python web-scraping python-3.x python-requests python-requests-html
我正在运行 @Dan-Dev 在他的回答中提供的代码。
from requests_html import HTMLSession
url = 'https://www.thefreedictionary.com/love'
session = HTMLSession()
r = session.get(url)
r.html.render()
lang_bar = r.html.find('#LangBar', first=True)
print(lang_bar.html)
Run Code Online (Sandbox Code Playgroud)
结果是
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-2-ec1d9137b197> in <module>
8
9 resp = session.get(url, headers = headers)
---> 10 resp.html.render()
11
12 soup = bs(resp.html.html, "lxml")
C:\Anaconda3\lib\site-packages\requests_html.py in render(self, retries, script, wait, scrolldown, sleep, reload, timeout, keep_page)
584 """
585
--> 586 self.browser = self.session.browser # Automatically create a event loop and browser
587 content …Run Code Online (Sandbox Code Playgroud) 我想抓取一个页面,其内容似乎是由 html 中引用的应用程序呈现的,例如:
<div id="app" class="app-mobile-pusher"></div>
Run Code Online (Sandbox Code Playgroud)
我正在使用 Requests-HTML python 库中的 render() 方法,如下所示:
with HTMLSession() as session:
p = session.post(login_url, data=payload)
r = session.get(content_url)
r.html.render()
print(r.text)
Run Code Online (Sandbox Code Playgroud)
此代码返回页面的 HTML,没有任何错误,但也没有任何内容(只有 HTML 标签)。笔记:
我已经尝试向 session.get 添加超时参数,以便在访问页面和上述语法的其他变体之前为页面提供更多时间来呈现。
还尝试根据此答案在标题中添加用户代理信息(为了避免拒绝我的自动抓取)
我第一次运行 render() 时,chrome 浏览器确实下载了
缺少任何错误消息让我感到困惑,并且很难复制此请求的上下文以在另一个站点上进行测试。
任何有关如何解决的具体建议,或有关如何进行故障排除的想法,表示赞赏。(Python 3.6,Mac 操作系统)
我有一个简单的 Flask API,其中一个端点调用另一个文件中的方法,以使用 request-html 从站点呈现一些 javascript
@app.route('/renderJavascript')
def get_attributes():
return get_item_attributes('https://www.site.com.mx/site.html')
Run Code Online (Sandbox Code Playgroud)
该方法的代码如下所示:
from requests_html import HTMLSession
from bs4 import BeautifulSoup
def get_item_attributes(url):
#Connecting to site.
session = HTMLSession()
resp = session.get(url)
resp.html.render()
resp.session.close()
soup = BeautifulSoup(resp.html.html,'lxml')
................................
#Rest of the code is handling the data with bs4 and returning a json.
Run Code Online (Sandbox Code Playgroud)
调用端点后,我收到此错误:
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python37\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python37\lib\site-packages\flask\app.py", line 1820, in …Run Code Online (Sandbox Code Playgroud) 在让我的抓取工具在另一个网站上正常工作后,我正在学习如何为另一个网站 Reverb.com 构建另一个抓取工具。然而,从混响中提取信息更具挑战性,并且使用我的旧刮刀的模型的工作方式并不相同。我做了一些研究,发现使用requests_html代替requests似乎是大多数人在 Javascript 中使用的选项,就像 Reverb.com 那样。
我本质上是试图抓取标题和价格信息的文本版本,然后对不同页面进行分页或循环访问 URL 列表以获取所有内容。我有点在那里,但遇到了障碍。下面是我正在摆弄的代码的两个版本。
下面的第一个版本打印出所有看起来只有 3 页内容的内容,但它打印出所有带有标记的乐器名称和价格。然而,在 CSV 中,所有这些项目仅一起打印在 3 行上,而不是每行 1 个项目/价格对。
from requests_html import HTMLSession
from bs4 import BeautifulSoup
import csv
from fake_useragent import UserAgent
session = HTMLSession()
r = session.get("https://reverb.com/marketplace/bass-guitars?year_min=1900&year_max=2022")
r.html.render(sleep=5)
soup = BeautifulSoup(r.html.raw_html, "html.parser")
#content scrape
b = soup.findAll("h4", class_="grid-card__title") #title
for i in b:
print(i)
p = soup.findAll("div", class_="grid-card__price") #price
for i in p:
print(i)
Run Code Online (Sandbox Code Playgroud)
相反,此版本仅将 3 行打印到 CSV,但名称和价格被删除了所有标记。但只有当我将 更改findAll为 just时才会发生这种情况find。我读到这 …
javascript python beautifulsoup web-scraping python-requests-html
我正在使用 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) 我试图检查链接是否包含http并打印URL.
import requests
from requests_html import HTMLSession
import sys
link = "http://www.tvil.me/view/93/4/8/v/%D7%90%D7%99%D7%99_%D7%96%D7%95%D7%9E%D7%91%D7%99_IZombie.html"
enter_episodes = HTMLSession().get(link)
page = enter_episodes.html
s = page.xpath("//*[@class='view-watch-button']/a")
for l in s:
link = l.links
if link != "set()":
print(link)
Run Code Online (Sandbox Code Playgroud)
响应:
{'http://streamcloud.eu/ga4m4hizbrfb/iZombie.S04E08.HDTV.x264-SVA.mkv.html'}
{'http://uptostream.com/77p26f7twwhe'}
set()
{'https://clipwatching.com/aog2ni06rzjt/rrFhepnbFfpt6xg.mkv.html'}
set()
[Finished in 1.7s]
Run Code Online (Sandbox Code Playgroud)
我试图删除set()响应并仅获取没有{'和的链接'}.
我html.render()从requests_html图书馆跑。它正在尝试安装铬,但我收到一个错误
我已经尝试pip install --upgrade certifi过和没有sudo
并得到:Requirement already up-to-date: certifi in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (2019.6.16)
我也尝试运行/Applications/Python\ 3.6/Install\ Certificates command并得到:
-- pip install --upgrade certifi
Requirement already up-to-date: certifi in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (2019.6.16)
-- removing any existing file or link
-- creating symlink to certifi certificate bundle
-- setting permissions
-- update complete
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
[W:pyppeteer.chromium_downloader] start chromium download.
Download may take a …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个简单的脚本,给定任意 URL 将返回该网站的标题标签。因为我要解析的许多 URL 需要启用 JavaScript,所以我需要使用诸如 requests_html 的渲染函数之类的东西来做到这一点。但是,我遇到了库的问题,其中下面的示例 URL 永远不会终止。我已经尝试了渲染调用的超时参数,但没有奏效。任何人都可以帮助我弄清楚如何正确地超时或其他一些解决方法以确保它不会卡住吗?
这是我当前不会终止的代码(它卡在渲染调用上):
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('http://shan-shui-inf.lingdong.works/')
# render with JS
r.html.render(sleep = 1, keep_page=True)
# Also does not work: r.html.render(sleep = 1, keep_page=True, timeout = 3)
title = r.html.find('title', first=True).full_text
Run Code Online (Sandbox Code Playgroud)
我已经尝试过类似的解决方案:函数调用超时和Python 超时装饰器仍然没有足够奇怪地超时。
注意:我在 Windows 10 上使用 Python 3.7.4 64 位。