使用 beautifulSoup 在元素中抓取数据
Scraping data in element using beautifulSoup
我正在使用 beautifulSoup 进行网络抓取。我设法抓取了名字,但问题是,如果数据在元素中,例如 phone 号码和电子邮件位于如下图所示,我真的不确定如何抓取:
我的代码:
import requests
from bs4 import BeautifulSoup
raw = requests.get('https://www.iproperty.com.my/property/findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=10&st=KL&ct=&st1=&ct1=#40091').text
raw = raw.replace("</br>", "")
soup = BeautifulSoup(raw, 'html.parser')
import re
phone = ['data-content'])[0][1:][:-1] for d in soup.find_all('a',{'class':'csagentphonelead'})]
name = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("p", class_='box-listing_agentCS')]
website = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentemaillead')]
num_page_items = len(name)
with open('results180.csv', 'a') as f:
for i in range(num_page_items):
f.write(name[i] + "," + phone[i] + "," + website[i] + "," + "\n")
我的抓取结果是 "Click for Email" 和 "Click for Phone"。
我应该修复什么,以便结果是正确的电子邮件和 phone 号码?
您必须从 link 中获取数据属性的值。你可以试试这个代码 -
import requests
from bs4 import BeautifulSoup
raw = requests.get('https://www.iproperty.com.my/property/findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=10&st=KL&ct=&st1=&ct1=#40091').text
raw = raw.replace("</br>", "")
soup = BeautifulSoup(raw, 'html.parser')
import re
#['data-content'])[0][1:][:-1] ## note sure what is this
# for d in soup.find_all('a',{'class':'csagentphonelead'}):
name = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("p", class_='box-listing_agentCS')]
phone = [x['data'].strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentphonelead')]
website = [x['data'].strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentemaillead')]
num_page_items = len(name)
with open('results180.csv', 'a') as f:
for i in range(num_page_items):
f.write(name[i] + "," + phone[i] + "," + website[i] + "," + "\n")
我正在使用 beautifulSoup 进行网络抓取。我设法抓取了名字,但问题是,如果数据在元素中,例如 phone 号码和电子邮件位于如下图所示,我真的不确定如何抓取:
我的代码:
import requests
from bs4 import BeautifulSoup
raw = requests.get('https://www.iproperty.com.my/property/findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=10&st=KL&ct=&st1=&ct1=#40091').text
raw = raw.replace("</br>", "")
soup = BeautifulSoup(raw, 'html.parser')
import re
phone = ['data-content'])[0][1:][:-1] for d in soup.find_all('a',{'class':'csagentphonelead'})]
name = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("p", class_='box-listing_agentCS')]
website = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentemaillead')]
num_page_items = len(name)
with open('results180.csv', 'a') as f:
for i in range(num_page_items):
f.write(name[i] + "," + phone[i] + "," + website[i] + "," + "\n")
我的抓取结果是 "Click for Email" 和 "Click for Phone"。 我应该修复什么,以便结果是正确的电子邮件和 phone 号码?
您必须从 link 中获取数据属性的值。你可以试试这个代码 -
import requests
from bs4 import BeautifulSoup
raw = requests.get('https://www.iproperty.com.my/property/findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=10&st=KL&ct=&st1=&ct1=#40091').text
raw = raw.replace("</br>", "")
soup = BeautifulSoup(raw, 'html.parser')
import re
#['data-content'])[0][1:][:-1] ## note sure what is this
# for d in soup.find_all('a',{'class':'csagentphonelead'}):
name = [x.text.strip().split("\r\n")[-1].strip() for x in soup.find_all("p", class_='box-listing_agentCS')]
phone = [x['data'].strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentphonelead')]
website = [x['data'].strip().split("\r\n")[-1].strip() for x in soup.find_all("a", class_='csagentemaillead')]
num_page_items = len(name)
with open('results180.csv', 'a') as f:
for i in range(num_page_items):
f.write(name[i] + "," + phone[i] + "," + website[i] + "," + "\n")