BeautifulSoup是否适用于Python 3?
如果没有,有多少港口?会有港口吗?
谷歌没有给我任何东西(也许它是'因为我正在寻找错误的东西?)
for imgsrc in Soup.findAll('img', {'class': 'sizedProdImage'}):
if imgsrc:
imgsrc = imgsrc
else:
imgsrc = "ERROR"
patImgSrc = re.compile('src="(.*)".*/>')
findPatImgSrc = re.findall(patImgSrc, imgsrc)
print findPatImgSrc
'''
<img height="72" name="proimg" id="image" class="sizedProdImage" src="http://imagelocation" />
Run Code Online (Sandbox Code Playgroud)
这就是我想从中提取的内容,我得到了:
findimgsrcPat = re.findall(imgsrcPat, imgsrc)
File "C:\Python27\lib\re.py", line 177, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
Run Code Online (Sandbox Code Playgroud)
"""
我试图从这个页面中提取信息.该页面一次加载10个项目,我需要滚动以加载所有条目(总共100个).我能够解析HTML并获取前10个条目所需的信息,但我想在解析HTML之前完全加载所有条目.
我正在使用python,requests和BeautifulSoup.我用前10个条目加载时解析页面的方式如下:
from bs4 import BeautifulSoup
import requests
s = requests.Session()
r = s.get('https://medium.com/top-100/december-2013')
page = BeautifulSoup(r.text)
Run Code Online (Sandbox Code Playgroud)
但这只会加载前10个条目.所以我查看了页面,得到了用于加载后续条目的AJAX请求,我得到了一个响应,但它是一个时髦的JSON,我宁愿使用HTML解析器而不是解析JSON.这是代码:
from bs4 import BeautifulSoup
import requests
import json
s = requests.Session()
url = 'https://medium.com/top-100/december-2013/load-more'
payload = {"count":100}
r = s.post(url, data=payload)
page = json.loads(r.text[16:]) #skip some chars that throw json off
Run Code Online (Sandbox Code Playgroud)
这给了我数据,但它是一个非常冗长和复杂的JSON,我宁愿加载页面上的所有数据,只需解析HTML.此外,呈现的HTML提供了比JSON响应更多的信息(即作者的名称而不是模糊的用户ID等).这里有类似的问题,但没有相关的答案.理想情况下,我想进行POST调用,然后请求HTML并解析它,但我无法做到这一点.
问题:当我尝试执行脚本时,BeautifulSoup(html, ...)给出错误消息"TypeError:类型'对象的对象'没有len().我尝试将实际的html作为参数传递,但它仍然不起作用.
import requests
url = 'http://vineoftheday.com/?order_by=rating'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, "html.parser")
Run Code Online (Sandbox Code Playgroud) 如何迭代Beautiful Soup元素的HTML属性?
喜欢,给定:
<foo bar="asdf" blah="123">xyz</foo>
Run Code Online (Sandbox Code Playgroud)
我想要"酒吧"和"等等".
我有一些看起来像这样的HTML:
<h1>Title</h1>
//a random amount of p/uls or tagless text
<h1> Next Title</h1>
Run Code Online (Sandbox Code Playgroud)
我想将所有HTML从第一个h1复制到下一个h1.我怎样才能做到这一点?
任何人都知道一种优雅的方式将汤对象的全部内容作为单个字符串?
在我得到的那一刻contents,这当然是一个列表,然后迭代它:
notices = soup.find("div", {"class" : "middlecontent"})
con = ""
for content in notices.contents:
con += str(content)
print con
Run Code Online (Sandbox Code Playgroud)
谢谢!
我使用BeautifulSoup for Python 3.3成功从网页中提取所需的信息.我还使用BeautifulSoup生成新的HTML代码来显示此信息.目前,我的Python程序打印出HTML代码,然后我必须将其复制,粘贴并保存为HTML文件,然后从那里,我可以在浏览器中测试它.
所以我的问题是,在Python中是否有办法在Web浏览器中启动BeautifulSoup生成的HTML代码,这样我就不必经历我现在使用的复制和粘贴方法?
我正在进行网络抓取项目,并遇到速度问题.为了尝试修复它,我想使用lxml而不是html.parser作为BeautifulSoup的解析器.我已经能够做到这一点:
soup = bs4.BeautifulSoup(html, 'lxml')
Run Code Online (Sandbox Code Playgroud)
但我不想'lxml'每次打电话给BeautifulSoup都要反复输入.有没有办法在程序开始时设置一次使用哪个解析器?
如果我的班级名字经常不同,比如说:
listing-col-line-3-11 dpt 41
listing-col-block-1-22 dpt 41
listing-col-line-4-13 CWK 12
Run Code Online (Sandbox Code Playgroud)
通常我可以这样做:
for EachPart in soup.find_all("div", {"class" : "ClassNamesHere"}):
print EachPart.get_text()
Run Code Online (Sandbox Code Playgroud)
有太多的类名可以在这里使用,所以其中一些是出来的.
我知道Python没有我通常会使用的".contains"但它确实有一个"in".虽然我还没有找到一种方法来融入它.
我希望有一种方法可以用正则表达式做到这一点.虽然我的Python语法真的让我失望但我一直在尝试变化:
regex = re.compile('.*listing-col-.*')
for EachPart in soup.find_all(regex):
Run Code Online (Sandbox Code Playgroud)
但这似乎并没有成功.
beautifulsoup ×10
python ×10
html ×5
python-3.x ×2
web-scraping ×2
html-parsing ×1
json ×1
lxml ×1
parsing ×1
porting ×1
regex ×1