标签: beautifulsoup

在Python中动态更改范围?

所以说我使用BeautifulSoup来解析页面,我的代码指出查询至少有7页.

分页看起来像

 1 2 3 4 5 6 7 Next
Run Code Online (Sandbox Code Playgroud)

如果我一直分页到7,有时会超过7页,所以如果我在第7页,分页看起来像

 1 2 3    7 8 9 10 Next
Run Code Online (Sandbox Code Playgroud)

所以现在,我知道至少还有3页.我正在使用初始传递来计算有多少页面,即get_num_pages返回7

我正在做的是迭代每页上的项目,所以我有类似的东西

for page in range(1,num_pages + 1):
  # do some stuff here
Run Code Online (Sandbox Code Playgroud)

如果脚本计算出超过7页,有没有办法动态更新范围?我想另一种方法是保持计数,当我到第7页时,单独处理.我正在寻找建议和解决方案,以最好的方式来解决这个问题.

python beautifulsoup

1
推荐指数
1
解决办法
1518
查看次数

为什么BeautifulSoup会抛出这个HTMLParseError?

我认为BeautifulSoup将能够处理格式错误的文档,但是当我发送页面的源代码时,会打印下面的回溯:


Traceback (most recent call last):
  File "mx.py", line 7, in 
    s = BeautifulSoup(content)
  File "build\bdist.win32\egg\BeautifulSoup.py", line 1499, in __init__
  File "build\bdist.win32\egg\BeautifulSoup.py", line 1230, in __init__
  File "build\bdist.win32\egg\BeautifulSoup.py", line 1263, in _feed
  File "C:\Python26\lib\HTMLParser.py", line 108, in feed
    self.goahead(0)
  File "C:\Python26\lib\HTMLParser.py", line 150, in goahead
    k = self.parse_endtag(i)
  File "C:\Python26\lib\HTMLParser.py", line 314, in parse_endtag
    self.error("bad end tag: %r" % (rawdata[i:j],))
  File "C:\Python26\lib\HTMLParser.py", line 115, in error
    raise HTMLParseError(message, self.getpos())
HTMLParser.HTMLParseError: bad end tag: u"", at line 258, column 34
Run Code Online (Sandbox Code Playgroud)

它应该不能处理这种东西吗?如果它可以处理它们,我怎么能这样做?如果没有,是否有一个可以处理格式错误的文件的模块? …

python parsing exception beautifulsoup malformed

1
推荐指数
1
解决办法
2845
查看次数

Beautifulsoup在表中获得价值

我试图刮 http://www.co.jefferson.co.us/ats/displaygeneral.do?sch=000104 并获得"所有者姓名"我的工作但是真的很难看而且不是最好的我确定,所以我正在寻找更好的方法.这是我有的:

soup = BeautifulSoup(url_opener.open(url))            
x = soup('table', text = re.compile("Owner Name"))
print 'And the owner is', x[0].parent.parent.parent.tr.nextSibling.nextSibling.next.next.next
Run Code Online (Sandbox Code Playgroud)

相关的HTML是

<td valign="top">
    <table border="1" cellpadding="1" cellspacing="0" align="right">
    <tbody><tr class="tableheaders">
    <td>Owner Name(s)</td>
    </tr>

    <tr>

    <td>PILCHER DONALD L                         </td>
    </tr>

    </tbody></table>
</td>
Run Code Online (Sandbox Code Playgroud)

哇,有很多关于beautifulsoup的问题,我看了看他们但找不到帮助我的答案,希望这不是一个重复的问题

python screen-scraping beautifulsoup html-content-extraction

1
推荐指数
1
解决办法
4235
查看次数

美丽的汤:从<ul>访问<li>元素,没有id

我试图从这个维基百科页面刮掉有生日的人

这是现有的代码:

hdr = {'User-Agent': 'Mozilla/5.0'}
site = "http://en.wikipedia.org/wiki/"+"january"+"_"+"1"
req = urllib2.Request(site,headers=hdr)    
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)

print soup
Run Code Online (Sandbox Code Playgroud)

这一切都很好,我得到整个HTML页面,但我想要特定的数据,我不知道如何使用没有id使用的Beautiful Soup访问它.该<ul>标签没有一个id也不做<li>标记.另外,我不能只询问每个<li>标签,因为页面上还有其他列表.是否有特定方式来调用给定列表?(我不能只为这一页使用修复程序,因为我计划迭代所有日期并让每个页面生日,我不能保证每个页面都与此页面完全相同).

python beautifulsoup html-parsing web-scraping

1
推荐指数
2
解决办法
1万
查看次数

使用BeautifulSoup添加元标记

如何使用Beautiful Soup(库)在HTML页面中的标题标记之后添加元标记.我使用python语言进行编码,但无法执行此操作.

python beautifulsoup python-2.7

1
推荐指数
1
解决办法
1644
查看次数

美丽的汤查找 - 获取文本

我有一些代码只是作为字符串吐出价格(125.01),但我必须改变一些东西,因为现在它用html标签和所有内容打印整行.如何在不使用正则表达式的情况下将其打印出文本?

import requests
from bs4 import BeautifulSoup

url = 'http://finance.yahoo.com/q?s=aapl&fr=uh3_finance_web&uhb=uhb2'

data = requests.get(url)
soup = BeautifulSoup(data.content)
price = soup.find("span", {'id':'yfs_l84_aapl'})
print(price)


<span id="yfs_l84_aapl">125.01</span>
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup web

1
推荐指数
1
解决办法
1108
查看次数

从html文档中提取日期

我有一个非常大的html文件.我需要抓取这个html文件并提取某些信息

soup.findAll('table',{"summary" : "This table displays snapshot information"})

[<table border="1" summary="This table displays snapshot information" width="500">
 <tbody><tr><th class="awrnobg" scope="col"></th><th class="awrbg" scope="col">Snap Id</th><th class="awrbg" scope="col">Snap Time</th><th class="awrbg" scope="col">Sessions</th><th class="awrbg" scope="col">Cursors/Session</th></tr>
 <tr><td class="awrnc" scope="row">Begin Snap:</td><td align="right" class="awrnc">98810</td><td align="center" class="awrnc">29-Jun-15 08:00:02</td><td align="right" class="awrnc">700</td><td align="right" class="awrnc">    129.6</td></tr>
 <tr><td class="awrc" scope="row">End Snap:</td><td align="right" class="awrc">98864</td><td align="center" class="awrc">29-Jun-15 17:00:23</td><td align="right" class="awrc">703</td><td align="right" class="awrc">    129.1</td></tr>
 <tr><td class="awrnc" scope="row">Elapsed:</td><td class="awrnc"> </td><td align="center" class="awrnc">             540.35 (mins)</td><td class="awrnc"> </td><td class="awrnc"> </td></tr>
 <tr><td class="awrc" scope="row">DB Time:</td><td class="awrc"> </td><td align="center" class="awrc">           2,963.17 (mins)</td><td …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

1
推荐指数
1
解决办法
669
查看次数

PyCharm无法导入BeautifulSoup

我试图在Python上使用bs4制作一个蜘蛛,我已经使用pip和easy_install安装了bs4,但是一旦我使用PyCharm运行程序,就会出错:

Traceback (most recent call last):
  File "C:/PyCharm Project/bs4.py", line 3, in <module>
    from bs4 import BeautifulSoup
  File "C:\PyCharm Project\bs4.py", line 3, in <module>
    from bs4 import BeautifulSoup
ImportError: cannot import name 'BeautifulSoup'

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

但是在cmd提示符中没有出现此错误:

C:\WINDOWS\system32>python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>>
>>> html_doc = """
... <html><head><title>The Dormouse's story</title></head>
... <body> …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

1
推荐指数
1
解决办法
9159
查看次数

使用BeautifulSoup获取&lt;tr&gt;标签的样式

我正在抓取页面,并从该页面上的表中获取所有<tr>元素,如下所示:

r = requests.get("http://lol.esportswikis.com/wiki/G2_Esports/Match_History")
s = BeautifulSoup(r.content, "lxml")
tr = s.find_all("table", class_="wikitable sortable")[0].find_all("tr")[3:]

print tr[0]
Run Code Online (Sandbox Code Playgroud)

输出:

<tr style="background-color:#C6EFCE"><td>...</td> ... <td>...</td></tr>
Run Code Online (Sandbox Code Playgroud)

现在,我试图获取<tr>标签的样式,但是我不知道如何。例如,如果我这样做:

for item in tr[0]:
    print item
Run Code Online (Sandbox Code Playgroud)

它显然只是打印<td> ... </td>东西。我想我大概可以做这样的事情print tr[0].something,比如tr[0].tag,但一切到目前为止,我已经尝试并没有带来我想要的东西。

python beautifulsoup python-requests

1
推荐指数
1
解决办法
5428
查看次数

用逗号和漂亮的汤代替逗号

在此输入图像描述

Html代码行如上.

我已经设法从这个网址获得它

import requests
from bs4 import BeautifulSoup as soup

url = 'https://www.saa.gov.uk/search/?SEARCHED=1&ST=&SEARCH_TERM=city+of+edinburgh%2C+BOSWALL+PARKWAY%2C+EDINBURGH&ASSESSOR_ID=&SEARCH_TABLE=valuation_roll_cpsplit&DISPLAY_COUNT=10&TYPE_FLAG=CP&ORDER_BY=PROPERTY_ADDRESS&H_ORDER_BY=SET+DESC&DRILL_SEARCH_TERM=BOSWALL+PARKWAY%2C+EDINBURGH&DD_TOWN=EDINBURGH&DD_STREET=BOSWALL+PARKWAY&UARN=110B60329&PPRN=000000000001745&ASSESSOR_IDX=10&DISPLAY_MODE=FULL#results'

baseurl = 'https://www.saa.gov.uk'
session = requests.session()
response = session.get(url)

# content of search page in soup 
html = soup(response.content,"lxml")

Address = LeftBlockData[3].get_text().strip()
print (Address)
Run Code Online (Sandbox Code Playgroud)

然而它打印像这样 '29 BOSWALL PARKWAYEDINBURGHEH5 2BR'

那里是是<br />文本之间的一个替代'no space'.

我想在目前的地方放一个逗号<br />.

请问有人可以推荐一种方法吗?

html python beautifulsoup

1
推荐指数
1
解决办法
415
查看次数