soup.find_all返回空列表

时间:2019-08-06 21:16:25

标签: python-3.x web-scraping beautifulsoup

我对python还是很陌生,我正在尝试搜索指定的邮政编码并打印相应的城市,但是我无法这样做。

from bs4 import BeautifulSoup
import requests
import html.parser

# I am using Chrome as default browser

url = ("http://google.co.uk/search?=sk71nd")
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

'''我以为下面的课程将返回以下城市:     城市:大曼彻斯特,布拉姆霍尔,斯托克波特大都会自治市镇,但我只是得到了一个空清单'''

print(soup.find_all("div", {'class': "zloOqf PZPZlf kno-fb-ctx"}))

是否可以搜索提供的邮政编码并打印生成的城市?非常感谢

1 个答案:

答案 0 :(得分:0)

尝试一下

from bs4 import BeautifulSoup
import requests
import html.parser
import re

# I am using Chrome as default browser
data = []
data2 = []
url = ("https://en.wikipedia.org/wiki/List_of_postcode_districts_in_the_United_Kingdom")
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
#print(soup.prettify())
table = soup.find("table", {"class": "toc"})
for row in table.findAll("tr"):
    cols = row.find_all('td')
    cols = [ele.text.strip() for ele in cols]
    data.append([ele for ele in cols if ele])

print(data[1])

table = soup.find("table", {"class": "wikitable sortable"})
for row in table.findAll("tr"):
    cols = row.find_all('td')
    cols = [ele.text.strip() for ele in cols]
    data2.append([ele for ele in cols if ele])

print(data2)
  

data1-存储邮政编码data2-[邮政编码区域,邮政编码   地区,前邮政县的邮政镇]