【发布时间】:2017-07-07 01:24:35
【问题描述】:
我正在使用 Python 3.5 并试图抓取一个 url 列表(来自同一网站),代码如下:
import urllib.request
from bs4 import BeautifulSoup
url_list = ['URL1',
'URL2','URL3]
def soup():
for url in url_list:
sauce = urllib.request.urlopen(url)
for things in sauce:
soup_maker = BeautifulSoup(things, 'html.parser')
return soup_maker
# Scraping
def getPropNames():
for propName in soup.findAll('div', class_="property-cta"):
for h1 in propName.findAll('h1'):
print(h1.text)
def getPrice():
for price in soup.findAll('p', class_="room-price"):
print(price.text)
def getRoom():
for theRoom in soup.findAll('div', class_="featured-item-inner"):
for h5 in theRoom.findAll('h5'):
print(h5.text)
for soups in soup():
getPropNames()
getPrice()
getRoom()
到目前为止,如果我打印汤、获取 propNames、getPrice 或 getRoom,它们似乎都可以工作。但我似乎无法通过每个 url 打印 getPropNames、getPrice 和 getRoom。
仅学习 Python 几个月,因此非常感谢您的帮助!
【问题讨论】:
标签: python web-scraping urllib bs4