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

时间:2021-02-22 19:20:55

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!

1 个解决方案

#1


1  

You need to, first, make a request using mechanize and only then pass the response to the BeautifulSoup for parsing:

首先,您需要使用mechanize发出请求,然后将响应传递给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

#1


1  

You need to, first, make a request using mechanize and only then pass the response to the BeautifulSoup for parsing:

首先,您需要使用mechanize发出请求,然后将响应传递给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