python xpath不返回任何数据

kr98yfug  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(283)

试图从列表中刮取数据,但由于某些原因,它返回空。类似的代码在其他网站上也起到了作用,我很困惑为什么它在这个网站上不起作用。请帮忙!

import requests
from lxml import html

start_url ="https://www.anybusiness.com.au/search?page=1&sort=date-new-old"
res = requests.get(start_url)
tree = html.fromstring(res.content) 

# Retrieve listing title

title_xpath = "/html/body/div[2]/section/div[2]/div/div[2]/div[2]/div/a/div/div/text()"
title_value= tree.xpath(title_xpath)

print(title_value)
>> []
elcex8rz

elcex8rz1#

import requests
from bs4 import BeautifulSoup
from pprint import pp

def main(url):
    params = {
        "page": 1,
        "sort": "date-new-old"
    }
    r = requests.get(url, params=params)
    soup = BeautifulSoup(r.text, 'lxml')
    pp([x.get_text(strip=True)
        for x in soup.select('div[style^="position: relative; padding: 10px; "]')])

main("https://www.anybusiness.com.au/search")

输出:

['COMING SOON Bar Under Management - $250k/month ave takings',
 'Mobile Windscreen and Auto Glass',
 '21168 Profitable Engineering Business – Coal Mining Industry',
 'Exciting Food Franchise Opportunities. Sydney.',
 'Exciting Food Franchise Opportunities. Brisbane.',
 'Pizza Hut Franchise Sale',
 'Luxury Horse Float Business – National Opportunity! (6543)',
 'Mobile Windscreen and Auto Glass Business. Offers over $220,000 WIWO',     
 'SIMPLE ESPRESSO BAR - HILLS DISTRICT - 00805.',
 'The Ox and Hound Bistro - 1P5306',
 'A new lifestyle awaits - own and operate your own business in a growing '  
 'market',
 'Join the Fernwood Fitness Franchise Family',
 'Rare Business Opportunity - Appliance Finance Business -Ipswich',
 'Make a  Lifestyle Change to Beautiful Bendigo/Huntly Newsagency and Store',
 'Immaculately Presented Motel in a Thriving Regional City - 1P5317M']

最新答复:

import requests
from bs4 import BeautifulSoup
from pprint import pp

def main(url):
    params = {
        "page": 1,
        "sort": "date-new-old"
    }
    r = requests.get(url, params=params)
    soup = BeautifulSoup(r.text, 'lxml')
    pp([(x.a['href'], x.select_one('.ellipsis').get_text(strip=True))
       for x in soup.select('.basic')])

main("https://www.anybusiness.com.au/search")

输出:

[('/listings/oakleigh-east-vic-3166-retail-food-beverage-3330830',
  "Portman's Continental Butcher & Deli"),
 ('/listings/melbourne-vic-3000-leisure-entertainment-bars-nightclubs-3330829',
  'COMING SOON Bar Under Management - $250k/month ave takings'),
 ('/listings/north-mackay-qld-4740-industrial-manufacturing-mining-earth-moving-manufacturing-engineering-3330827',
  '21168 Profitable Engineering Business – Coal Mining Industry'),
 ('/listings/food-hospitality-cafe-coffee-shop-3330826',
  'Exciting Food Franchise Opportunities. Sydney.'),
 ('/listings/food-hospitality-cafe-coffee-shop-3330825',
  'Exciting Food Franchise Opportunities. Brisbane.'),
 ('/listings/alderley-qld-4051-food-hospitality-takeaway-food-3330824',
  'Pizza Hut Franchise Sale'),
 ('/listings/castle-hill-nsw-2154-food-hospitality-cafe-coffee-shop-takeaway-food-retail-food-beverage-3330821',
  'SIMPLE ESPRESSO BAR - HILLS DISTRICT - 00805.'),
 ('/listings/beechworth-vic-3747-food-hospitality-restaurant-cafe-coffee-shop-retail-food-beverage-3330820',
  'The Ox and Hound Bistro - 1P5306'),
 ('/listings/west-launceston-tas-7250-services-cleaning-home-garden-home-based-franchise-3330819',
  'A new lifestyle awaits - own and operate your own business in a growing '
  'market'),
 ('/listings/leisure-entertainment-sports-complex-gym-recreation-sport-franchise-3330818',
  'Join the Fernwood Fitness Franchise Family'),
 ('/listings/melbourne-vic-3000-professional-finance-services-hire-rent-retail-homeware-hardware-3330817',
  'Rare Business Opportunity - Appliance Finance Business -Ipswich'),
 ('/listings/huntly-north-vic-3551-retail-newsagency-tatts-food-hospitality-convenience-store-office-supplies-3330816',
  'Make a  Lifestyle Change to Beautiful Bendigo/Huntly Newsagency and Store')]

相关问题