美丽的汤刮图案?
我试图刮包含以下HTML代码网站:美丽的汤刮图案?
<div class="content-sidebar-wrap"><main class="content"><article
class="post-773 post type-post status-publish format-standard has-post-
thumbnail category-money entry" itemscope
itemtype="http://schema.org/CreativeWork">
这包含数据我感兴趣......我一直在使用BeautifulSoup解析它尝试过,但以下回报:
<div class="content-sidebar-wrap"><main class="content"><article
class="entry">
<h1 class="entry-title">Not found, error 404</h1><div class="entry-content
"><p>"The page you are looking for no longer exists. Perhaps you can return
back to the site's "<a href="http://www.totalsportek.com/">homepage</a> and
see if you can find what you are looking for. Or, you can try finding it
by using the search form below.</p><form
action="http://www.totalsportek.com/" class="search-form"
itemprop="potentialAction" itemscope=""
itemtype="http://schema.org/SearchAction" method="get" role="search">
# I've made small modifications to make it readable
美丽的汤元素不包含我想要的代码。我不太熟悉html,但我假设这会调用一些外部服务来返回数据..?我读过这个与Schema有关的东西。
无论如何我可以访问这些数据吗?
您在发出请求时需要指定User-Agent
标头。打印文章标题和内容的工作示例:
import requests
from bs4 import BeautifulSoup
url = "http://www.totalsportek.com/money/barcelona-player-salaries/"
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36"})
soup = BeautifulSoup(response.content, "html.parser")
article = soup.select_one(".content article.post.entry.status-publish")
header = article.header.get_text(strip=True)
content = article.select_one(".entry-content").get_text(strip=True)
print(header)
print(content)
您的代码有效。但是,我在自己的代码中指定了您的用户代理,但它仍然无效。除了设置用户代理以外,还必须做其他事情吗?运行一个简单的soup.table将不会返回任何应该至少有一个表的地方。 –
此外,我更喜欢直接访问html表格,而不是将其解析为文本 –
了解了表格....我只是使用您的代码。如果可能,我想知道为什么soup.find不起作用 –
您想从HTML代码中获得什么? –
一个html表。试图解析表格直接返回一个无 –
嗯我还是不明白,你试图从中获取信息的网站到底是什么?如果信息是由JavaScript构建的,“requests”将不起作用。 –