这就是我的代码,非常简单。由于某种原因,出现上述错误。即使我删除text = str(html)并替换soup = BeautifulSoup(text, 'html.parser')为同样的错误soup = BeautifulSoup(html, 'html.parser')。这是怎么回事?
with urllib.request.urlopen('https://jalopnik.com/search?q=mazda&u=&zo=-07:00') as response:
html = response.read()
text = str(html)
soup = BeautifulSoup(text, 'html.parser')
print(type(soup))
soup = soup.prettify()
print(soup.find_all('div'))
Run Code Online (Sandbox Code Playgroud) 我有以下代码,它为我提供了 Example.html 文件中的数据。但我必须逐行读取数据
html_doc = open("Example.html","r")
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.get_text())
Run Code Online (Sandbox Code Playgroud) 在我的项目中,我从亚马逊抓取数据。我将其部署在 Pythonanywhere 上(我使用付费帐户)。但是有一个问题,当我在 Pythonanywhere 上尝试时,代码(我使用的是 BeautifulSoup4)无法获取网站的 html。它获得了亚马逊的“出问题了”网站。但在我本地它工作得很好。我认为它与用户代理有关。在我的本地,我使用我自己的用户代理。部署时我应该使用哪个用户代理?我该如何解决这个问题?
这是我的代码:
URL = link ##some amazon link
headers = {"User-Agent": " ##my user agent"}
page = requests.get(URL, headers=headers)
soup1 = BeautifulSoup(page.content, 'html.parser')
soup2 = BeautifulSoup(soup1.prettify(), "html.parser")
Run Code Online (Sandbox Code Playgroud)
有什么办法可以在 Pythonanywhere 上做到这一点吗?
嗨我在标记中抓取一些东西时有疑问.通过bs4,我可以在下面的div标签之前联系.但我真正需要的是data-lat和data-lng.那些不是文本所以我不能使用get_text(),而且我不确定bs4中的哪个辅助函数可以提取标记中的那些内部数据.
<div id="map" class="main_content embedded-content" data-lat="37.542560322393925"
data-lng="127.01606371950948">
Run Code Online (Sandbox Code Playgroud) 是否有一个select表达式用于匹配多个类的标签?
请考虑以下HTML代码段:
<div id="top">
<div class="foo bar"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以与之匹敌soup.select('div#top div.foo')或soup.select('div#top div.bar').
但我需要两个班级都在那里.
有表达吗?
我试图从Zillow收集数据是不成功的.
例:
url = https://www.zillow.com/homes/for_sale/Los-Angeles-CA_rb/?fromHomePage=true&shouldFireSellPageImplicitClaimGA=false&fromHomePageTab=buy
我想从洛杉矶的所有家庭中提取地址,价格,zestimates,地点等信息.
我已经尝试使用像BeautifulSoup这样的包进行HTML抓取.我也尝试过使用json.我几乎肯定Zillow的API没有帮助.我的理解是,API最适合收集特定属性的信息.
我已经能够从其他站点获取信息,但似乎Zillow使用动态ID(更改每次刷新)使得访问该信息变得更加困难.
更新: 尝试使用以下代码,但仍然没有产生任何结果
import requests
from bs4 import BeautifulSoup
url = 'https://www.zillow.com/homes/for_sale/Los-Angeles-CA_rb/?fromHomePage=true&shouldFireSellPageImplicitClaimGA=false&fromHomePageTab=buy'
page = requests.get(url)
data = page.content
soup = BeautifulSoup(data, 'html.parser')
for li in soup.find_all('div', {'class': 'zsg-photo-card-caption'}):
try:
#There is sponsored links in the list. You might need to take care
#of that
#Better check for null values which we are not doing in here
print(li.find('span', {'class': 'zsg-photo-card-price'}).text)
print(li.find('span', {'class': 'zsg-photo-card-info'}).text)
print(li.find('span', {'class': 'zsg-photo-card-address'}).text)
print(li.find('span', {'class': 'zsg-photo-card-broker-name'}).text)
except :
print('An error occured')
Run Code Online (Sandbox Code Playgroud) 使用 beautiful soup 和 requests 库抓取 Facebook 或 Twitter 等带有大量 html 标签的大型网站时出现问题。
from bs4 import BeautifulSoup
import requests
html_text = requests.get('https://twitter.com/elonmusk').text
soup = BeautifulSoup(html_text, 'lxml')
elon_tweet = soup.find_all('span', class_='css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0')
print(elon_tweet)
Run Code Online (Sandbox Code Playgroud)
全跨度图像
当代码执行时,它返回一个空列表。
我是网络抓取的新手,欢迎详细解释。
我创建了一个脚本来使用请求模块从该网站收集不同的公司名称,但是当我执行该脚本时,它最终什么也没得到。我在页面源中查找了公司名称,发现这些名称在那里可用,因此它们似乎是静态的。
import requests
from bs4 import BeautifulSoup
link = 'https://clutch.co/agencies/digital-marketing'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
with requests.Session() as s:
s.headers.update(headers)
res = s.get(link)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select("h3.company_info > a"):
print(item.text)
Run Code Online (Sandbox Code Playgroud) python beautifulsoup web-scraping python-3.x python-requests
我正在尝试抓取以下网站
我注意到在页面之间导航期间对以下端点执行了 XHR POST 请求,如您在以下打印屏幕中所见:
在 POST 请求中卡住的东西,我注意到后面有一个动态值,GBK-但我不明白它是从哪里生成的或如何获取它的。
如果您只是在页面之间导航,您会注意到值保持变化。
根据以下回答更新Life is complex:
这是POST向 API发送请求的方法:
import requests
# we need the value!
url = "http://app1.nmpa.gov.cn/data_nmpa/face3/search.jsp?6SQk6G2z=GBK-Value"
# here you can add headers as you need!
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"cache-control": "no-cache",
"Content-Type": "application/x-www-form-urlencoded",
"Pragma": "no-cache"
}
data = {
"tableId": "27",
"State": [
"1",
"1",
"1",
"1",
"1",
"1",
"1" …Run Code Online (Sandbox Code Playgroud) 03420 <a href="/kegg-bin/show_pathway?ban03420">Nucleotide excision repair</a><br>
03430 <a href="/kegg-bin/show_pathway?ban03430">Mismatch repair</a><br>
03440 <a href="/kegg-bin/show_pathway?ban03440">Homologous recombination</a><br>
</ul>
</ul>
<!-- -->
<b>Environmental Information Processing</b>
<ul>
Membrane transport
<ul>
02010 <a href="/kegg-bin/show_pathway?ban02010">ABC transporters</a><br>
Run Code Online (Sandbox Code Playgroud)
我需要使用 python 从网页中提取路径代码(例如 03420、03430 等),这是我使用 Beautifulsoup 完成的。我想在环境信息处理之前停止,所以我正在寻找一些可以使用的不同标签。<!-- -->处于完美的位置,但我不知道如何在此时停止。有人可以告诉我是否/如何使用它来停止提取评论之前的代码。(我对 python 和 html 很陌生,直接跳到网络解析,所以请耐心等待。)
我正在尝试使用 Requests 和 BeautifulSoup在https://www.twitch.tv/directory/game/Dota%202上获取流的标题。我知道我的搜索条件是正确的,但我的程序没有找到我需要的元素。
这是一个屏幕截图,显示了浏览器中源代码的相关部分:
作为文本的 HTML 源代码:
<div class="tw-media-card-meta__title">
<div class="tw-c-text-alt">
<a class="tw-full-width tw-interactive tw-link tw-link--button tw-link--hover-underline-none tw-link--inherit" data-a-target="preview-card-title-link" href="/weplayesport_en">
<div class="tw-align-items-start tw-flex">
<h3 class="tw-ellipsis tw-font-size-5" title="NAVI vs HellRaisers | BO5 | ODPixel & S4 | WeSave! Charity Play">NAVI vs HellRaisers | BO5 | ODPixel & S4 | WeSave! Charity Play</h3>
</div>
</a>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import requests
from bs4 import BeautifulSoup
req = requests.get("https://www.twitch.tv/directory/game/Dota%202")
soup = BeautifulSoup(req.content, "lxml")
title_elems = soup.find_all("h3", attrs={"title": True})
print(title_elems)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,title_elems …