假设我正在抓取一个网页,并且我想选择网页上的某个图像。正如您可以根据类名称查找元素一样,我想通过其标签来选择图像src。如何选择我已经知道标签的图像src?
即我想选择标签src为:
https://assets.bandsintown.com/images/pin.svg
Run Code Online (Sandbox Code Playgroud) 我正在尝试从包含以下许多 div 的网页中提取内容(显然所有数据都具有不同的数据,除了初始部分):
<div data-asin="B007R2E578" data-index="0"
class="sg-col-20-of-24 s-result-item sg-col-0-of-12 sg-col-28-of-32 sg-col-16-of-20 AdHolder sg-col sg-col-32-of-36 sg-col-12-of-16 sg-col-24-of-28">
<div class="sg-col-inner">
Run Code Online (Sandbox Code Playgroud)
所有这些 div 的开头都相同:<div data-asin=
我正在尝试使用 Beautifulsoup 中的 find_all 函数提取所有这些:
structure = soup.find_all('div','data-asin=')
Run Code Online (Sandbox Code Playgroud)
但是它总是返回一个空列表。
我不想使用正则表达式。
BeautifulSoup中是否有任何函数可以获取所有这些div?
我试图让漂亮的汤在 python 3 的 cli 中运行,这样我就可以尝试并找出如何最好地使用它。我是通过pip安装的。
pip3 list
Run Code Online (Sandbox Code Playgroud)
节目
Package Version
-------------- -------
beautifulsoup4 4.8.0
Run Code Online (Sandbox Code Playgroud)
然而,当我运行 python3 CLI 并尝试导入 beautiful soup 来玩时,我收到错误:
>>> import beautifulsoup4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'beautifulsoup4'
Run Code Online (Sandbox Code Playgroud)
我还尝试通过 CLI 导入 bs4,但这很奇怪:
>>> import bs4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/derekchapman/Google Drive/Code/Python/p4e/bs4/__init__.py", line 48
'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This …Run Code Online (Sandbox Code Playgroud) 我正在尝试抓取网站,有时会收到此错误,这令人担忧,因为我随机收到此错误,但在重试后,我没有收到此错误。
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='www.somewebsite.com', port=443): Read timed out. (read timeout=None)
Run Code Online (Sandbox Code Playgroud)
我的代码如下所示
from bs4 import BeautifulSoup
from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem
import requests
software_names = [SoftwareName.CHROME.value]
operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.LINUX.value]
user_agent_rotator = UserAgent(software_names=software_names, operating_systems=operating_systems, limit=100)
pages_to_scrape = ['https://www.somewebsite1.com/page', 'https://www.somewebsite2.com/page242']
for page in pages_to_scrape:
time.sleep(2)
page = requests.get(page, headers={'User-Agent':user_agent_rotator.get_random_user_agent()})
soup = BeautifulSoup(page.content, "html.parser")
# scrape info
Run Code Online (Sandbox Code Playgroud)
正如您从我的代码中看到的,我什至使用 Time 使我的脚本休眠几秒钟,然后再请求另一个页面。我还使用随机的 user_agent。我不确定是否可以做任何其他事情来确保我永远不会收到“读取超时”错误。
我也遇到过这个,但他们似乎建议向标题添加额外的值,但我不确定这是否是一个通用的解决方案,因为这可能必须根据网站的具体情况而定。我还在另一篇SO Post上读到,我们应该对请求进行 Base64 处理并重试。这让我很困惑,因为我不知道该怎么做,而且这个人也没有提供例子。
任何有刮擦经验的人的建议将不胜感激。
这里的代码很好地展示了如何从https://www.investing.com/economic-calendar/中提取经济数据。然而,该代码只是从今天的当前经济数据表中抓取数据。有一个日历图标,可以手动设置时间范围并可以查看过去的经济事件(屏幕截图中以红色突出显示):
有没有办法抓取过去的经济事件数据?到目前为止,我还没有真正的想法......
有没有更简单的方法可以从此网址的html中查找坐标= https://www.sreality.cz/detail/prodej/byt/1+kk/praha-zizkov-krasova/151897164
我检查了该网站,但到目前为止似乎没有任何坐标。该网站正在使用mapy.cz
我尝试将地址转换为坐标,但坐标有时会关闭。
这是我尝试过的:
address = 'praha zizkov krasova'
url = 'https://nominatim.openstreetmap.org/search/' + urllib.parse.quote(address) +'?format=json'
response = requests.get(url).json()
print(response[0]["lat"])
print(response[0]["lon"])
Run Code Online (Sandbox Code Playgroud) >>> from bs4 import BeautifulSoup as bs
>>> html = """<html>
<head><title>Test</title></head>
<body>
<p><b>para1</b><\p>
<p><b>para2</b><\p>
</body>
</html>"""
>>>
>>> soup = bs(html)
>>> html == str(soup)
False
>>> print html
<html>
<head><title>Test</title></head>
<body>
<p><b>para1</b><\p>
<p><b>para2</b><\p>
</body>
</html>
>>> print soup
<html>
<head><title>Test</title></head>
<body>
<p><b>para1</b><\p>
<p><b>para2</b><\p>
</p></p></body>
</html>
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,html和str(soup)... 之间存在差异.
< became <
> became >
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我在上个月左右一直在学习python的基础知识,虽然我非常擅长打印'hello world',但我想学习一些额外的功能.我已下载BeautifulSoup4并使用Python2.7.我的目标是能够从CNN或其他新闻来源获取文章并能够废弃4件事:1)链接到网站2)发布日期文章3)文章标题4)文章文本
我已经在stackoverflow中搜索了其他问题并查看了其他示例代码,但是我将它应用于我想要做的事情时遇到了问题.我看到的大多数例子都是在刮擦时间或天气.我的主要问题是,当我查看特定网站的源代码时,我很难知道我应该使用哪些标签.
例如,如果我想废弃以上4件事:http: //www.cnn.com/2013/10/29/us/florida-shooting-cell-phone-blocks-bullet/index.html?http = ju_c2
代码会是什么样的?
我正在编写一个脚本,当我最喜欢的YouTuber Casey Neistat上传新视频时,该脚本最终能够通过Twitter帐户发推文.但是,为了做到这一点,我写了一个程序,当它识别出之前的YouTube链接列表时,应该能够将所有链接的"output.txt"文件与之前的视频进行比较.不包括最近上传的视频.我做了两个方法,一个叫做'mainloop',一遍又一遍地运行,看看所有Casey Neistat视频的前一个列表是否与从'getNeistatNewVideo'方法中检索到的一串新链接相同.然而,我遇到的问题是,一旦程序识别出一个新视频,就会转到方法'getNewURL',该方法将采用'output.txt'文件中记录的第一个链接.但是,当我说要打印这个新网址时,它说没有任何内容.我的预感是,这是因为python没有足够快地读取和写入output.txt文件,但是我可能错了.
我的代码如下:
import bs4
import requests
import re
import time
import tweepy
'''
This is the information required for Tweepy
CONSUMER_KEY =
CONSUMER_SECRET =
ACCESS_KEY =
ACCESS_SECRET =
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
End of Tweepy Information
'''
root_url = 'https://www.youtube.com/'
index_url = root_url + 'user/caseyneistat/videos'
def getNeistatNewVideo():
response = requests.get(index_url)
soup = bs4.BeautifulSoup(response.text)
return [a.attrs.get('href') for a in soup.select('div.yt-lockup-thumbnail a[href^=/watch]')]
def mainLoop():
results = str("\n".join(getNeistatNewVideo()))
past_results = open('output.txt').read()
if results == past_results: …Run Code Online (Sandbox Code Playgroud) 我试图从Tripadvisor获取一些评分数据,但是当我试图获取数据时,我得到了
“ NoneType”对象不可下标
谁能帮我弄清楚我要去哪里错了,对不起,我对python很陌生。
这是我的示例代码
import requests
import re
from bs4 import BeautifulSoup
r = requests.get('http://www.tripadvisor.in/Hotels-g186338-London_England-Hotels.html')
data = r.text
soup = BeautifulSoup(data)
for rate in soup.find_all('div',{"class":"rating"}):
print (rate.img['alt'])
Run Code Online (Sandbox Code Playgroud)
输出如下:
4.5 of 5 stars
4.5 of 5 stars 4 of 5 stars
4.5 of 5 stars
4.5 of 5 stars 4 of 5 stars
4.5 of 5 stars
4.5 of 5 stars
4.5 of 5 stars Traceback (most recent call last):
File "<ipython-input-52-7460e8bfcb82>", line 3, in <module>
print (rate.img['alt']) …Run Code Online (Sandbox Code Playgroud) beautifulsoup ×10
python ×10
web-scraping ×5
html ×2
css ×1
pip ×1
python-3.x ×1
string ×1
urllib ×1
xpath ×1