问题
I'm trying to extract the email address of each restaurant on TripAdvisor.
I've tried this but keeps returning an [ ]:
response.xpath('//*[@class= "restaurants-detail-overview-cards-LocationOverviewCard__detailLink--iyzJI restaurants-detail-overview-cards-LocationOverviewCard__contactItem--89flT6"]')
Code snippet off the TripAdvisor page is below:
<div class="restaurants-detail-overview-cards-LocationOverviewCard__detailLink--iyzJI restaurants-detail-overview-cards-LocationOverviewCard__contactItem--1flT6"><span><a href="mailto:info@canopylounge.my?subject=?"><span class="ui_icon email restaurants-detail-overview-cards-LocationOverviewCard__detailLinkIcon--T_k32"></span><span class="restaurants-detail-overview-cards-LocationOverviewCard__detailLinkText--co3ei">Email</span><span class="ui_icon external-link-no-box restaurants-detail-overview-cards-LocationOverviewCard__upLinkIcon--1oVn1"></span></a></span></div>
回答1:
First: you had mistake in class name.
Second: it is class in <div> but @href is in <a>. And <a> is not directly after <div> so you need
'//*[@class="..."]//a/@href'
(I skip class name because it is too long to display it)
But instead of so long class name you can try
'//a[contains(@href, "mailto")]/@href'
I tested xpath using lxml
text = '''<div class="restaurants-detail-overview-cards-LocationOverviewCard__detailLink--iyzJI restaurants-detail-overview-cards-LocationOverviewCard__contactItem--1flT6">
<span><a href="mailto:info@canopylounge.my?subject=?">
<span class="ui_icon email restaurants-detail-overview-cards-LocationOverviewCard__detailLinkIcon--T_k32"></span>
<span class="restaurants-detail-overview-cards-LocationOverviewCard__detailLinkText--co3ei">Email</span>
<span class="ui_icon external-link-no-box restaurants-detail-overview-cards-LocationOverviewCard__upLinkIcon--1oVn1"></span>
</a></span>
</div>'''
import lxml.html
soup = lxml.html.fromstring(text)
print(soup.xpath('//*[@class="restaurants-detail-overview-cards-LocationOverviewCard__detailLink--iyzJI restaurants-detail-overview-cards-LocationOverviewCard__contactItem--1flT6"]//a/@href'))
print(soup.xpath('//a[contains(@href, "mailto")]/@href'))
回答2:
This is one of the ways how you can:
import requests
from scrapy import Selector
site_link = 'https://www.tripadvisor.com/Restaurant_Review-g60713-d11882449-Reviews-Coin_Op_Game_Room-San_Francisco_California.html'
res = requests.get(site_link)
sel = Selector(res)
email = sel.xpath("//*[contains(@class,'LocationOverviewCard__contactItem--')]//a[contains(@href,'mailto:')]/@href").get()
email = email.split("mailto:")[1].split("?")[0] if email else ""
print(email)
Output:
info@coinopsf.com
回答3:
Selector also has a .re() method for extracting data using regular expressions.
In [2]: response.xpath('//a[contains(@href, "mailto")]/@href')
Out[2]: [<Selector xpath='//a[contains(@href, "mailto")]/@href' data='mailto:info@coinopsf.com?subject=?'>]
In [3]: response.xpath('//a[contains(@href, "mailto")]/@href').get()
Out[3]: 'mailto:info@coinopsf.com?subject=?'
In [4]: response.xpath('//a[contains(@href, "mailto")]/@href').re('mailto:(.*)\?\w')
Out[4]: ['info@coinopsf.com']
In [5]: response.xpath('//a[contains(@href, "mailto")]/@href').re('mailto:([^?]*)')
Out[5]: ['info@coinopsf.com']
来源:https://stackoverflow.com/questions/58426462/how-do-i-extract-the-email-address-using-scrapy