【问题标题】:Using BeautifulSoup to submit button/expand aria-expandable使用 BeautifulSoup 提交按钮/展开 aria-expandable
【发布时间】:2021-04-28 04:18:22
【问题描述】:

您好,我正在尝试扩展此 page 上的按钮,以使用漂亮的汤来捕获剩余的属性。这是我要按下的按钮的 html:

<button class="button__373c0__3lYgT secondary__373c0__1bsQo" aria-expanded="false" aria-controls="expander-link-content-cf6b4b45-8720-4627-96f8-397a766b8ddb" type="submit" value="submit" style="--mousedown-x:30px; --mousedown-y:27.625px; --button-width:113.422px;"><div class=" button-content__373c0__1QNtB border-color--default__373c0__3-ifU"><span class=" text__373c0__2Kxyz button-content-text__373c0__Z-7FO text-color--inherit__373c0__1lczC text-align--center__373c0__3VrfZ text-weight--semibold__373c0__2l0fe text-size--large__373c0__3t60B text--truncated__373c0__3sLaf"><p class=" text__373c0__2Kxyz text-color--normal__373c0__3xep9 text-align--left__373c0__2XGa- text-weight--semibold__373c0__2l0fe text-size--large__373c0__3t60B">15 More Attributes</p></span></div></button>

这就是我目前所拥有的:

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9'}
url = 'https://www.yelp.com/biz/lazi-cow-davis?osq=lazi+cow'
response = requests.get(url, headers=headers)

sub_response = requests.get(sub_url, headers=headers)
sub_soup = BeautifulSoup(sub_response.content, 'lxml')

for item in sub_soup.select('section'):
    if item.find('h4'):
       name = item.find('h4').get_text()
       if name == "Amenities and More":
          tests = item.find_all('span')
          for span in tests:
              print(span.get_text())

我知道您可以使用 Yelp API 进行抓取,但我需要对 +1000 个不同的 Yelp 网站执行此操作,所以我想知道是否有解决方法,因为我目前的方法有效(我稍后会添加代理) , 只是不适用于所有属性

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup urlopen


    【解决方案1】:

    BeautifulSoup 主要仅用于网页抓取,仅此而已。

    如果您需要与网页交互,例如点击按钮、滚动等,您需要使用浏览器。 会帮助你

    【讨论】:

      【解决方案2】:

      您可以尝试使用 Selenium:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      
      driver = webdriver.Chrome('/Users/Downloads/chromedriver')
      
      driver.implicitly_wait(30)
      
      driver.find_element_by_xpath("//button[@class='button__373c0__3lYgT secondary__373c0__1bsQo activated__373c0__1moG8 ']").click()
      
      for x in driver.find_elements_by_xpath("//div[@class=' arrange__373c0__2C9bH gutter-2__373c0__1DiLQ vertical-align-baseline__373c0__3HGi9 border-color--default__373c0__3-ifU']"):
          print(x.text)
      

      如果这有帮助,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-21
        • 1970-01-01
        • 2011-06-29
        • 2013-11-23
        • 1970-01-01
        • 2020-06-15
        • 2012-02-10
        相关资源
        最近更新 更多