简体   繁体   English

如何使用Python从HTML中提取它?

[英]How to extract it from HTML with Python?

I am trying to extract that data: 我正在尝试提取该数据:

<div class="address"><h3>Text1</h3><div class="adr">Text2</div></div>

I want to print text1 and text2. 我想打印text1和text2。

I try this: 我尝试这样:

br = mechanize.Browser()
html = """<div class="address"><h3></h3><div class="adr"></div></div>"""
soup = BeautifulSoup(html)
br.addheaders = [('User-agent', 'Firefox')]
br.open("http://de.fakenamegenerator.com/gen-male-gr-gr.php")
adress = soup.findAll('div', attrs={ "class" : "adr"})

print len(adress)

I am getting as Result: 我得到的结果是:

1

...

Thanks! 谢谢!

You need to, first, make a request using mechanize and only then pass the response to the BeautifulSoup for parsing: 首先,您需要使用mechanize进行请求,然后将response传递给BeautifulSoup进行解析:

from bs4 import BeautifulSoup
import mechanize

br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.122 Safari/537.36')]
br.open("http://de.fakenamegenerator.com/gen-male-gr-gr.php")

soup = BeautifulSoup(br.response())
address = soup.find('div', attrs={"class": "address"})

print address.h3.text.strip()
print address.find('div', attrs={'class': 'adr'}).text.strip()

Prints: 打印:

Jan Amsel
Invalidenstrasse 6567159 Friedelsheim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM