python - 在 python 中存储 html

标签 python html beautifulsoup lxml lxml.html

我同时使用 xpath 和 beautifulsoup 来抓取网页。 Xpath 需要 tree 作为输入,而 beautifulsoup 需要 soup 作为输入。 这是获取树和汤的代码:

def get_tree(url):
    r = requests.get(url)
    tree = html.fromstring(r.content)
    return tree

# get soup
def get_soup(url):
    r = requests.get(url)
    data = r.text
    soup = BeautifulSoup(data)
    return soup

这两种方法都使用 requests.get(url)。这就是我要提前存储的内容。 这是 python 中的代码:

import requests
url = "http://www.nytimes.com/roomfordebate/2013/10/28/should-you-bribe-your-kids"
r = requests.get(url)
f = open('html','wb')
f.write(r)

然后我得到这样的错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be convertible to a buffer, not Response

这是存储文本的代码,但出现错误:

import requests
from lxml import html
url = "http://www.nytimes.com/roomfordebate/2013/02/13/when-divorce-is-a-family-affair"
r = requests.get(url)
c = r.content
outfile = open("html", "wb")
outfile.write(c)
outfile.close()
infile = open("html", "rb")
tree = html.fromstring(infile)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/lxml/html/__init__.py", line 662, in fromstring
    start = html[:10].lstrip().lower()
TypeError: 'file' object has no attribute '__getitem__'

我该如何解决这个问题?

最佳答案

infile = open("html", "rb") #this is a file object Not a string

您需要先使用 read() 阅读它,而不仅仅是打开 :-)-

infile = open("html", "rb")
infile=infile.read()
tree = html.fromstring(infile)

关于python - 在 python 中存储 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26810651/

相关文章:

python - 从拆分中返回至少 X 个结果

javascript - iPad 上的两栏网页布局

java - 如何使用输入类型文本在页面加载时显示空数据表

jquery - 想要在不同的 div 框中单击按钮时显示列表

python - 编辑 BS4,当网页中没有表格时不返回任何内容

python - 如何将生成器表达式作为 Python 的 join 方法的输入?

python - 在 Ubuntu 18 中安装 Auto Sklearn

python - lxml中 `etree.fromstring()`和 `etree.XML()`的功能区别是什么?

python - 为包含 _dopostback 方法的多个页面抓取网站,并且页面的 URL 不会更改

python - 使用 BeautifulSoup4 和 Python 3.3 解析错误