【发布时间】:2021-05-05 16:22:13
【问题描述】:
这是交易。我要抓取 257 页数据。我写了一个简单的代码,它使用 selenium 打开网站,抓取第一页的必要文本。一切正常,创建了我需要的数据框,一切都很好。代码如下:
url = 'www.sampleurl.com'
driver.get(url)
driver.set_window_size(800,500)
name = driver.find_elements_by_class_name('pflist-itemtitle')
address = driver.find_elements_by_class_name('pflist-address')
contact = driver.find_elements_by_css_selector("a[href*='tel:']")
name_list = []
for a in range(len(dealer_name)):
name_list.append(name[a].text)
address_list = []
for b in range(len(address)):
address_list.append(address[b].text)
contact_list = []
for c in range(len(contact)):
contact_list.append(contact[c].text)
data_tuples = list(zip(name_list[0:], address_list[0:], contact_list[0:])) # everything is paired together
temp_df = pd.DataFrame(data_tuples, columns=['Dealer Name', 'Address', 'Contact']) #creates temporary dataframe of each tuple in the field_listingtype
df = df.append(temp_df)
print('Scraping done on this page')
print('moving to next page')
我无法找到与 selenium 一起使用的 Next 按钮的好 ID,单击并移至下一页以重复该过程,等等接下来的 257 页。
我正在查看 f' 字符串以在 url 中所需位置传递页码,但我被卡住了。任何指针将不胜感激:
def new_page():
for page in range(2,257):
next_page = f'https://www.samplepage.com/page/{THIS IS WHERE PAGE# WOULD BE INSERTED}/?field_listingtype=104&field527110067894682300000%5B0%5D=215&field527110067894682300000%5B1%5D=203&field527110067894682300000%5B2%5D=212&field527110067
driver.get(next_page)
print(page)
【问题讨论】:
-
如何手动跳转到下一页?
标签: python selenium web-scraping