【问题标题】:How to click one by one to get data from website by using selenium python如何使用selenium python一一点击从网站获取数据
【发布时间】:2018-08-28 17:22:59
【问题描述】:

我正在尝试从网站获取数据,但我想选择第一个打开的 1000 个链接并从那里获取数据。

我试过了:

list_links = driver.find_elements_by_tag_name('a')

for i in list_links:
        print (i.get_attribute('href')) 

通过这个获得不需要的额外链接。

例如:https://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1,2,3,4,5,%3E5&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment,Residential-House,Villa,Residential-Plot&cityName=Mumbai

我们将获得超过 50k 的链接。如何仅打开前 1000 个链接在下面带有属性照片。

编辑

我也试过这个:

driver.find_elements_by_xpath("//div[@class='.l-srp__results.flex__item']")
driver.find_element_by_css_selector('a').get_attribute('href')

for matches in driver:
    print('Liking')
    print (matches)
    #matches.click()
    time.sleep(5)

但出现错误:TypeError: 'WebDriver' object is not iterable

为什么我没有通过使用此行获得链接:driver.find_element_by_css_selector('a').get_attribute('href')

编辑 1

我正在尝试按以下方式对链接进行排序,但出现错误

            result = re.findall(r'https://www.magicbricks.com/propertyDetails/', my_list)
            print (result)

错误:TypeError:预期的字符串或类似字节的对象

或尝试过

            a = ['https://www.magicbricks.com/propertyDetails/']
            output_names = [name for name in a if (name[:45] in my_list)]
            print (output_names)

什么都没有。

所有链接都在列表中。请推荐

提前谢谢你。请推荐

【问题讨论】:

  • 你能给我们一个你想要的链接的例子吗?您需要优化选择器。
  • 请打开此链接https://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1,2,3,4,5,%3E5&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment,Residential-House,Villa,Residential-Plot&cityName=Mumbai,在这里您将获得超过50000个房产详细信息,然后单击第一个https://www.magicbricks.com/propertyDetails/2-BHK-1182-Sq-ft-Multistorey-Apartment-FOR-Sale-Kandivali-East-in-Mumbai&id=4d423336313032373731,然后您将看到一些数据,例如卧室、浴室等。

标签: python python-3.x selenium web-scraping web-crawler


【解决方案1】:

Selenium 对于网页抓取不是一个好主意。我建议您使用免费且开源的 JMeter。

http://www.testautomationguru.com/jmeter-how-to-do-web-scraping/

如果您想使用 selenium,您尝试遵循的方法不是一种稳定的方法 - 单击并抓取数据。相反,我建议你遵循这个 - 这里有类似的东西。这个例子是在java中。但是你可以理解。

driver.get("https://www.yahoo.com");

Map<Integer, List<String>> map = driver.findElements(By.xpath("//*[@href]")) 
                .stream()                             // find all elements which has href attribute & process one by one
                .map(ele -> ele.getAttribute("href")) // get the value of href
                .map(String::trim)                    // trim the text
                .distinct()                           // there could be duplicate links , so find unique
                .collect(Collectors.groupingBy(LinkUtil::getResponseCode)); // group the links based on the response code

更多信息在这里。

http://www.testautomationguru.com/selenium-webdriver-how-to-find-broken-links-on-a-page/

【讨论】:

    【解决方案2】:

    我相信你应该收集列表中标签名称为“a”且“href”属性不为空的所有元素。
    然后遍历列表,逐个点击元素。
    创建一个 WebElement 类型的列表并存储所有有效链接。
    在这里您可以应用更多过滤器或条件,即链接包含一些字符或其他条件。

    要将 WebElement 存储在列表中,您可以使用 driver.findEelements() 此方法将返回 WebElement 类型的列表。

    【讨论】:

    • 如何对链接进行排序?我只需要一些有效的链接。请建议
    • 在这种情况下,我想给你的数据喜欢。有效链接是什么意思,无效链接是什么意思? .举个例子。
    猜你喜欢
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多