首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组中的字符串/ for循环中的列表

数组中的字符串/ for循环中的列表
EN

Stack Overflow用户
提问于 2019-11-19 22:14:24
回答 1查看 73关注 0票数 0

所以我的数组包含了这些数据

代码语言:javascript
复制
Abonnenten = ["https://www.instagram.com/vivksj/",
          "https://www.instagram.com/aaalink/",
          "https://www.instagram.com/haanannnnna/",
          "https://www.instagram.com/tiigergirlxofical/",
          "https://www.instagram.com/patriiciaa.015/",
          "https://www.instagram.com/itss_leonie_/",
          "https://www.instagram.com/patriciawk/",
          "https://www.instagram.com/taelly.scr/",
          "https://www.instagram.com/mozzhliana/",
          "https://www.instagram.com/mialecdron/"]

由于数据保护,我更改了用户名,但您得到了示例。在我的代码中,我想浏览一些选定的instagram用户。如果他们的snapchat像这样链接在他们的个人简历中:

Snapchat名称示例1

Snapchat名称示例2

代码语言:javascript
复制
for i in range(len(Abonnenten)):
driver.get(Abonnenten[i])
# get the text from their instagram bio
try:
    wait = WebDriverWait(driver, 10)
    bio = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='-vDIg']/span"))).text
    Snapnames = list.append(bio)
    print(Snapnames)

在这里,我想尝试获取我的应用程序打印出来的所有snapchat名称。但我搞不懂。

代码语言:javascript
复制
 # check if text contains "snapchat"
    if ("snapchat" in bio):

        # split the instagram bio by newline char to get line with snapchat name
        bio_lines = bio.split("\n")

        # parse over the instagram bio to find snapchat username
        for line in bio_lines:

            # if we find the line with username, strip out text to get the username
            if ("Snapchat:" in line):
                #snapchat_username = []
                snapchat_username = line.replace[("Snapchat:", "")]
                # you probably need to do something here to save to file
                print(snapchat_username)

尽管这些代码行都有注释,但我不知道它们是做什么的,也不知道它们是否工作。

代码语言:javascript
复制
        # case: the user does not have a bio, so just move on to the next one
except TimeoutException:
    continue
i = i + 1

代码的最后一行应该是自我解释。再一次,我想在instagram用户的个人简历中搜索的snapchat名称,如果他们有这个名字,它会将其保存到一个列表或数组中,这样我就可以把它作为一个文件提供出来,或者打印出来。(我用发色器)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-20 19:41:39

这应该会让你开始,它会创建一个用户名列表及其相关的bio。

代码语言:javascript
复制
import requests

profile_links = ["https://www.instagram.com/vivksj/", 
                 "https://www.instagram.com/aaalink/",
                 "https://www.instagram.com/haanannnnna/",
                 "https://www.instagram.com/tiigergirlxofical/",
                 "https://www.instagram.com/patriiciaa.015/",
                 "https://www.instagram.com/itss_leonie_/",
                 "https://www.instagram.com/patriciawk/",
                 "https://www.instagram.com/taelly.scr/",
                 "https://www.instagram.com/mozzhliana/",
                 "https://www.instagram.com/mialecdron/"]

profile_data_list = []

for curr_profile_url in profile_links:
    req_res = requests.get(curr_profile_url + '?__a=1')
    if req_res.status_code != 200:
        print(f'Error status code {req_res.status_code} for url {curr_profile_url}')
    else:
        json_res = req_res.json()
        json_user_portion = json_res['graphql']['user']
        username = json_user_portion['username']
        profile_data_list.append((username, json_user_portion))

profile_bio_list = [(curr_username, curr_data['biography']) for curr_username, curr_data in profile_data_list]

profile_bio_list看起来是这样的:

代码语言:javascript
复制
[('vivksj',
  "I just don't walk on the clouds of my dreams,I turn them into reality..."),
 ('aaalink', ''),
 ('itss_leonie_',
  '❤️14 y/o❤️\n'
  '15.8 B-day❤️\n'
  '@itss_celia_ ❤️\n'
  '@itss_maria_4 ❤️\n'
  '@leonie__prv_'),
 ('patriciawk', '')]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58943644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档