标签: beautifulsoup

用什么漂亮的汤 findall 正则表达式字符串?

我在表单的 HTML 中有链接

<a href="/downloadsServlet?docid=abc" target="_blank">Report 1</a>
<a href="/downloadsServlet?docid=ixyz" target="_blank">Fetch Report 2 </a>
Run Code Online (Sandbox Code Playgroud)

我可以使用 BeautifulSoup 获取上述表单的链接列表

我的代码如下

from bs4 import BeautifulSoup
html_page = urllib2.urlopen(url)
soup = BeautifulSoup(html_page)
listOfLinks = list(soup.findall('a'))
Run Code Online (Sandbox Code Playgroud)

但是,我想在引用链接的文本中找到包含“Fetch”一词的链接。

我试过表格

soup.findAll('a', re.compile(".*Fetch.*"))
Run Code Online (Sandbox Code Playgroud)

但这行不通。如何仅选择具有 href 且文本部分中包含“Fetch”一词的标签 a?

python regex beautifulsoup web-scraping

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

Python-beautifulsoup-如何处理丢失的结束标签

我想使用 beautifulsoup 从 html 代码中抓取表格。html 的一个片段如下所示。使用时,table.findAll('tr')我得到整个表,而不仅仅是行。(可能是因为 html 代码中缺少结束标记?)

  <TABLE COLS=9 BORDER=0 CELLSPACING=3 CELLPADDING=0>
  <TR><TD><B>Artikelbezeichnung</B>
  <TD><B>Anbieter</B>
  <TD><B>Menge</B>
  <TD><B>Taxe-EK</B>
  <TD><B>Taxe-VK</B>
  <TD><B>Empf.-VK</B>
  <TD><B>FB</B>
  <TD><B>PZN</B>
  <TD><B>Nachfolge</B>

  <TR><TD>ACTIQ 200 Mikrogramm Lutschtabl.m.integr.Appl.
  <TD>Orifarm
  <TD ID=R>     30 St
  <TD ID=R>  266,67
  <TD ID=R>  336,98
  <TD>&nbsp;
  <TD>&nbsp;
  <TD>12516714
  <TD>&nbsp;

  </TABLE>
Run Code Online (Sandbox Code Playgroud)

这是我的python代码,用于显示我正在努力解决的问题:

     soup = BeautifulSoup(data, "html.parser")
     table = soup.findAll("table")[0]
     rows = table.find_all('tr')
     for tr in rows:
         print(tr.text)
Run Code Online (Sandbox Code Playgroud)

python html-table beautifulsoup

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

使用 BeautifulSoup 提取图片标题和图片网址

我正在尝试使用 BeautifulSoup 从文章中提取图像 url 和图像标题。我可以将文章的图片 url 和图片标题与前后 HTML 分开,但我不知道如何将这两个与它们的 html 标签分开。这是我的代码:

from bs4 import BeautifulSoup
import requests
url = 'http://www.prnewswire.com/news-releases/dutch-philosopher-
koert-van-mensvoort-founder-of-the-next-nature-network-writes-a-
letter-to-humanity-619925063.html'
r = requests.get(url)
html = r.text
soup = BeautifulSoup(html, 'lxml')
links = soup.find_all('div', {'class': 'image'})
Run Code Online (Sandbox Code Playgroud)

我试图提取的两个部分是 src= 和 title= 部分。任何关于如何完成这两个解析的想法将不胜感激。

html python parsing beautifulsoup

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

Errno 13 权限被拒绝:'/Library/Python/2.7/site-packages/test-easy-install-18954.pth'

我已经下载了 python 3.6.1,我正在尝试使用终端来设置beautifulsoup4,但它一直试图在 python 2.7 上安装。有什么帮助吗?

Jakes-iMac:beautifulsoup4-4.5.3 Jake$ cd /Users/Jake/Downloads/beautifulsoup4-4.5.3 Jakes-iMac:beautifulsoup4-4.5.3 Jake$ python setup.py install running install

检查 .pth 文件支持 /Library/Python/2.7/site-packages/

error: can't create or remove files in install directory
Run Code Online (Sandbox Code Playgroud)

尝试添加或删除安装目录中的文件时出现以下错误:

[Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-18954.pth'
Run Code Online (Sandbox Code Playgroud)

您指定的安装目录(通过 --install-dir、--prefix 或 distutils 默认设置)是:

/Library/Python/2.7/site-packages/
Run Code Online (Sandbox Code Playgroud)

也许您的帐户没有对该目录的写访问权限?如果安装目录是系统拥有的目录,您可能需要以管理员或“root”帐户登录。如果您没有对这台机器的管理访问权限,您可能希望选择不同的安装目录,最好是在您的 PYTHONPATH 环境变量中列出的目录。

有关其他选项的信息,您可能希望查阅以下文档:

https://pythonhosted.org/setuptools/easy_install.html

请对您的系统进行适当的更改,然后重试

python beautifulsoup python-2.7

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

Python从网站上抓取表格?

我想抓取 treasury.gov 网站上提供的所有国债收益率。

https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldAll

我将如何获取这些信息?我假设我必须使用 BeautifulSoup 或 Selenium 或类似的东西(最好是 BS4)。我最终想将这些数据放入 Pandas DataFrame 中。

python selenium beautifulsoup dataframe pandas

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

抓取时拒绝访问

我想创建一个脚本以继续访问https://www.size.co.uk/featured/footwear/并抓取内容,但不知何故,当我运行脚本时,访问被拒绝。这是代码:

from urllib import urlopen
from bs4 import BeautifulSoup as BS
url = urlopen('https://www.size.co.uk/')
print BS(url, 'lxml')
Run Code Online (Sandbox Code Playgroud)

输出是

<html><head>
<title>Access Denied</title>
</head><body>
<h1>Access Denied</h1>

You don't have permission to access "http://www.size.co.uk/" on this server.
<p>
Reference #18.6202655f.1498945327.11002828
</p></body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我在其他网站上尝试时,代码运行良好,而且当我使用 Selenium 时,没有任何反应,但我仍然想知道如何在不使用 Selenium 的情况下绕过此错误。但是当我在http://www.footpatrol.co.uk/shop等不同网站上使用 Selenium 时,我遇到了相同的访问被拒绝错误,这是footpatrol 的代码:

from selenium import webdriver

driver = webdriver.PhantomJS('C:\Users\V\Desktop\PY\web_scrape\phantomjs.exe')
driver.get('http://www.footpatrol.com')
pageSource = driver.page_source
soup = BS(pageSource, 'lxml')
print soup
Run Code Online (Sandbox Code Playgroud)

输出是:

<html><head>
<title>Access Denied</title>
</head><body>
<h1>Access Denied</h1>

You don't have permission to …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup web-scraping

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

Python Beautiful Soup 提取 HTML 元数据

我得到了一些我不太明白的奇怪行为。我希望有人可以解释发生了什么。

考虑这个元数据:

<meta property="og:title" content="This is the Tesla Semi truck">
<meta name="twitter:title" content="This is the Tesla Semi truck">
Run Code Online (Sandbox Code Playgroud)

此行成功找到所有“og”属性并返回一个列表。

opengraphs = doc.html.head.findAll(property=re.compile(r'^og'))
Run Code Online (Sandbox Code Playgroud)

然而,这条线不能为 twitter 卡做同样的事情。

twitterCards = doc.html.head.findAll(name=re.compile(r'^twitter'))
Run Code Online (Sandbox Code Playgroud)

为什么第一行成功找到了所有的“og”(opengraph卡),却找不到推特卡?

html python twitter beautifulsoup web-scraping

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

美汤-多属性搜索

使用 Beautiful Soup,我想在 HTML 代码中找到所有标签“a”,其属性“class”等于“button btn-1-small”或属性“class”等于“button beta-callout btn” -1-小”。我的代码是:

alltd = soup.find_all('a', {'class':'button btn-1-small'}, {'class':'button 
beta-callout btn-1-small'})
Run Code Online (Sandbox Code Playgroud)

当我只有一个条件(“类”的一个值)时,我的代码可以完美运行,但是当我想要两个条件时,它就不再起作用了。

你知道如何写条件“OR”吗?为了有一个类的值,或者另一个

非常感谢。

液晶显示器

python tags parsing attributes beautifulsoup

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

导入错误:无法导入名称“BeautifulSoup”

为什么我得到 ImportError: cannot import name 'BeautifulSoup'

  line 1, in <module>
        from bs4 import BeautifulSoup
    ImportError: cannot import name 'BeautifulSoup'
Run Code Online (Sandbox Code Playgroud)

是否已安装?

pip install --upgrade --force-reinstall beautifulsoup4
Collecting beautifulsoup4
  Using cached beautifulsoup4-4.6.0-py3-none-any.whl
Installing collected packages: beautifulsoup4
  Found existing installation: beautifulsoup4 4.6.0
    Uninstalling beautifulsoup4-4.6.0:
      Successfully uninstalled beautifulsoup4-4.6.0
Successfully installed beautifulsoup4-4.6.0
Run Code Online (Sandbox Code Playgroud)

看来如此。

python beautifulsoup python-3.x

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

使用 python、请求和 bs4 进行亚马逊价格网络抓取

我有一个关于网络抓取亚马逊文章价格的问题。我试图获得一篇文章的价格,但不幸的是它并不总是有效。我随机收到状态代码 503(服务器不可用)。我可以通过 while 循环解决这个问题,如果状态代码 == 200 则结束。我想了解不可用服务器的主要问题,所以我可以解决主要问题而不是解决它。到目前为止,该问题仅发生在亚马逊上。

这是我进行 10 次测试的代码。请求通常会失败 2/10 次

import requests
from bs4 import BeautifulSoup


for i in range(10):
    page = requests.get("https://www.amazon.de/Bloodborne-Game-Year-PlayStation-4/dp/B016ZU4FIQ/ref=sr_1_3?ie=UTF8&qid=1519566642&sr=8-3&keywords=bloodborne+ps4")

    if page.status_code != 200:
        print("Error status code: " + str(page.status_code))
        continue

    soup = BeautifulSoup(page.content, "html.parser")

    price = soup.find(id="priceblock_ourprice", class_="a-size-medium a-color-price")


    price_string = price.get_text()

    print(price_string)
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup python-requests

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