【发布时间】:2021-03-07 20:49:56
【问题描述】:
如何构建我的代码以包含有时在运行循环中的分页。我还没有使用def。也许这就是答案。但是在找了几天之后,我被卡住了。我正在寻找一个高水平的答案。
这是我的基本结构:
import requests_html
from requests_html import HTMLSession
from requests_html import HTML
import re
import pandas as pd
from time import sleep
from random import randint
session = HTMLSession()
from url retrieve links for states with providers
(loop) from states retrieve urls for cities with providers
from city_page in cities:
Retrieve data about individual providers from each "card"
(max 10 "cards" per page) Everything is fine up to this point.
Here is where I am lost. Sometimes a city only has one or two providers and that works
just fine. Sometimes there are 50+ providers at 10 per page. There are links to the
extra pages at the bottom of the container/ [<element>].
我一般如何构建我的代码以在出现额外页面时循环它们?然后返回到包含循环,以便它可以继续到下一个城市?我不知道如何将额外的 url 注入现有循环。
【问题讨论】:
-
建议你开始做这件事并让一些东西发挥作用,然后——并且只有在需要时——看看是否有机会改进代码。
-
谢谢巴尼。我已经尝试过了,目前我正在尝试一个新的 def 函数,将抓取循环变成一个 def(我对这些仍然非常陌生),然后尝试在两种情况下调用它(仅主页和/或存在的子页面)并在可用时迭代或遍历。我不喜欢在这里提问,这么多严厉的回答会责备提问者。所以当我在这里问的时候,我感觉就像撞到了墙一样。感谢您的鼓励,非常感谢。
标签: python python-3.x pandas python-requests-html