【发布时间】: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