【发布时间】:2020-04-20 19:36:55
【问题描述】:
我有一个 csv 文件,其中包含我需要抓取的链接。我还设置了使用相同的 chrome 浏览器进行登录(我需要的元素仅在登录时可用)。当我在循环之外抓取单个页面时,我会从页面中获得我需要的结果。当我将相同的代码放入循环中以抓取所有链接时,我会得到不同的结果。我认为这与“source =”和或“soup =”有关
CSV 文件包含 3 个链接:
https://www.redfin.com/UT/Murray/875-E-Arrowhead-Ln-84107/unit-44/home/77418264
https://www.redfin.com/UT/Murray/35-W-American-Ave-84107/home/86446505
https://www.redfin.com/UT/Murray/4551-S-200-E-84107/home/86457987
单页代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
#####################################
chrome_driver = "C:/chromedriver.exe"
Chrome_options = Options()
Chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9015")
driver = webdriver.Chrome(chrome_driver, options=Chrome_options)
#####################################
driver.get("https://www.redfin.com/UT/Murray/4551-S-200-E-84107/home/86457987")
source = driver.page_source
soup = BeautifulSoup(source, "html.parser")
#####################################
address = soup.find('span', class_='street-address').text
print(" Address: " + address)
city = soup.find('span', class_='locality').text
print(" City: " + city)
state = soup.find('span', class_='region').text
print(" State: " + state)
zipcode = soup.find('span', class_='postal-code').text
print(" ZipCode: " + zipcode)
soldPrice = soup.find('div', class_='price-col number').text
print(" Sold Price: " + soldPrice)
ln = soup.find('div', class_='listing-agent-item')
Lname = ln.find_all('span')[1].text
print("Listing Agent: " + Lname)
bn = soup.find('div', class_='buyer-agent-item')
Bname = bn.find_all('span')[1].text
print(" Buying Agent: " + Bname)
date = soup.find('div',attrs={"class":"col-4"})
sDate = date.find_all('p')[0].text
print(" Date: " + sDate)
mls = soup.find('div', class_='sourceContent').text
print(" MLS Source: " + mls)
for span in soup.find_all('span'):
if span.find(text='MLS#'):
mlsNum = span.nextSibling.text
print(" MLS#: " + mlsNum)
driver.quit()
单页结果完美显示:
Address: 4551 S 200 E
City: Murray,
State: UT
ZipCode: 84107
Sold Price: $262,000
Listing Agent: Jerold Ivie
Buying Agent: Zac Eldridge
Date: Dec 20, 2019
MLS Source: WFRMLS
MLS#: 1635000
[Finished in 3.3s]
循环前带有“source=”和“driver=”的循环代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import csv
#####################################
chrome_driver = "C:/chromedriver.exe"
Chrome_options = Options()
Chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9015")
driver = webdriver.Chrome(chrome_driver, options=Chrome_options)
#####################################
#driver.get("https://www.redfin.com/UT/Murray/4551-S-200-E-84107/home/86457987")
source = driver.page_source
soup = BeautifulSoup(source, "html.parser")
#####################################
with open('UTlinks.csv') as file:
readCSV = csv.reader(file)
for row in readCSV:
url = str(row).replace("['","").replace("']","")
print("_________________________________")
print("Scraping: " + url)
driver.get(url)
#source = driver.page_source
#soup = BeautifulSoup(source, "html.parser")
####################################
try:
address = soup.find('span', class_='street-address').text
print(" Address: " + address)
except:
print(" Address: " + "NA")
try:
city = soup.find('span', class_='locality').text
print(" City: " + city)
except:
print(" City: " + "NA")
try:
state = soup.find('span', class_='region').text
print(" State: " + state)
except:
print(" State: " + "NA")
try:
zipcode = soup.find('span', class_='postal-code').text
print(" ZipCode: " + zipcode)
except:
print(" ZipCode: " + "NA")
try:
soldPrice = soup.find('div', class_='price-col number').text
print(" Sold Price: " + soldPrice)
except:
print(" Sold Price: " "NA")
try:
ln = soup.find('div', class_='listing-agent-item')
Lname = ln.find_all('span')[1].text
print("Listing Agent: " + Lname)
except:
print("Listing Agent: " + "NA")
try:
bn = soup.find('div', class_='buyer-agent-item')
Bname = bn.find_all('span')[1].text
print(" Buying Agent: " + Bname)
except:
print(" Buying Agent: " + "NA")
try:
date = soup.find('div',attrs={"class":"col-4"})
sDate = date.find_all('p')[0].text
print(" Date: " + sDate)
except:
print(" Date: " + "NA")
try:
mls = soup.find('div', class_='sourceContent').text
print(" MLS Source: " + mls)
except:
print(" MLS Source: " + "NA")
try:
for span in soup.find_all('span'):
if span.find(text='MLS#'):
mlsNum = span.nextSibling.text
print(" MLS#: " + mlsNum)
except:
print(" MLS#: " + "NA")
循环结果: 您可以看到它打印文件中的 url,然后刮掉当前打开的浏览器结果 3 次......但抓取打开的 url 所需的所有信息。
_________________________________
Scraping: https://www.redfin.com/UT/Murray/875-E-Arrowhead-Ln-84107/unit-44/home/77418264
Address: 4551 S 200 E
City: Murray,
State: UT
ZipCode: 84107
Sold Price: $262,000
Listing Agent: Jerold Ivie
Buying Agent: Zac Eldridge
Date: Dec 20, 2019
MLS Source: WFRMLS
MLS#: 1635000
_________________________________
Scraping: https://www.redfin.com/UT/Murray/35-W-American-Ave-84107/home/86446505
Address: 4551 S 200 E
City: Murray,
State: UT
ZipCode: 84107
Sold Price: $262,000
Listing Agent: Jerold Ivie
Buying Agent: Zac Eldridge
Date: Dec 20, 2019
MLS Source: WFRMLS
MLS#: 1635000
_________________________________
Scraping: https://www.redfin.com/UT/Murray/4551-S-200-E-84107/home/86457987
Address: 4551 S 200 E
City: Murray,
State: UT
ZipCode: 84107
Sold Price: $262,000
Listing Agent: Jerold Ivie
Buying Agent: Zac Eldridge
Date: Dec 20, 2019
MLS Source: WFRMLS
MLS#: 1635000
[Finished in 6.9s]
如果我将 'source=' 和 'soup =' 放入循环中:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
#####################################
chrome_driver = "C:/chromedriver.exe"
Chrome_options = Options()
Chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9015")
driver = webdriver.Chrome(chrome_driver, options=Chrome_options)
#####################################
#driver.get("https://www.redfin.com/UT/Murray/4551-S-200-E- 84107/home/86457987")
#source = driver.page_source
#soup = BeautifulSoup(source, "html.parser")
#####################################
with open('UTlinks.csv') as file:
readCSV = csv.reader(file)
for row in readCSV:
url = str(row).replace("['","").replace("']","")
print("_________________________________")
print("Scraping: " + url)
driver.get(url)
source = driver.page_source
soup = BeautifulSoup(source, "html.parser")
####################################
try:
address = soup.find('span', class_='street-address').text
print(" Address: " + address)
except:
print(" Address: " + "NA")
try:
city = soup.find('span', class_='locality').text
print(" City: " + city)
except:
print(" City: " + "NA")
try:
state = soup.find('span', class_='region').text
print(" State: " + state)
except:
print(" State: " + "NA")
try:
zipcode = soup.find('span', class_='postal-code').text
print(" ZipCode: " + zipcode)
except:
print(" ZipCode: " + "NA")
try:
soldPrice = soup.find('div', class_='price-col number').text
print(" Sold Price: " + soldPrice)
except:
print(" Sold Price: " "NA")
try:
ln = soup.find('div', class_='listing-agent-item')
Lname = ln.find_all('span')[1].text
print("Listing Agent: " + Lname)
except:
print("Listing Agent: " + "NA")
try:
bn = soup.find('div', class_='buyer-agent-item')
Bname = bn.find_all('span')[1].text
print(" Buying Agent: " + Bname)
except:
print(" Buying Agent: " + "NA")
try:
date = soup.find('div',attrs={"class":"col-4"})
sDate = date.find_all('p')[0].text
print(" Date: " + sDate)
except:
print(" Date: " + "NA")
try:
mls = soup.find('div', class_='sourceContent').text
print(" MLS Source: " + mls)
except:
print(" MLS Source: " + "NA")
try:
for span in soup.find_all('span'):
if span.find(text='MLS#'):
mlsNum = span.nextSibling.text
print(" MLS#: " + mlsNum)
except:
print(" MLS#: " + "NA")
'source =' & 'soup =' 在循环结果中:
Scraping: https://www.redfin.com/UT/Murray/875-E-Arrowhead-Ln-84107/unit-44/home/77418264
Address: 875 E Arrow Head Ln S #44
City: Salt Lake City,
State: UT
ZipCode: 84107
Sold Price: NA
Listing Agent: Joe Olschewski
Buying Agent: James Corey
Date: NA
MLS Source: WFRMLS
MLS#: 1654937
_________________________________
Scraping: https://www.redfin.com/UT/Murray/35-W-American-Ave- 84107/home/86446505
Address: 35 American Ave
City: Murray,
State: UT
ZipCode: 84107
Sold Price: NA
Listing Agent: Dana Conway
Buying Agent: Rich Varga
Date: NA
MLS Source: WFRMLS
MLS#: 1660023
_________________________________
Scraping: https://www.redfin.com/UT/Murray/4551-S-200-E-84107/home/86457987
Address: 4551 S 200 E
City: Murray,
State: UT
ZipCode: 84107
Sold Price: NA
Listing Agent: Jerold Ivie
Buying Agent: Zac Eldridge
Date: NA
MLS Source: WFRMLS
MLS#: 1635000
[Finished in 8.6s]
现在它工作正常,但没有抓取“Sold Price:”或“Sold Date:”。如果我关闭错误处理,它会抛出这个错误:
soldPrice = soup.find('div', class_='price-col number').text
AttributeError: 'NoneType' object has no attribute 'text'
我在这里做错了什么?
【问题讨论】:
-
查看第一个链接时,我发现这个元素不包含数字。 soup.find('div', class_='price-col number')
-
@Sri,不确定您的意思?使用相同的选择器在循环外也能正常工作。
-
我刚刚运行它,但它在这一行不起作用。 soldPrice = soup.find('div', class_='price-col number').text.我得到 NoneType 对象没有属性文本,这意味着它找不到该元素。
-
@Sri,ZipCode后面的数据只有在登录的情况下才可用。你可以从第一块代码和结果中看到,它在那里并找到它。甚至是第二个代码块。不明白为什么只有这两个元素让我在循环中感到悲伤
标签: python-3.x selenium beautifulsoup