简体   繁体   English

使用 python 将行转换为列表

[英]transform lines into a list with python

Developing my algorithm, which is below:开发我的算法,如下所示:

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


browser =webdriver.Firefox(executable_path=r'C:/path/geckodriver.exe')
browser.get('https://brainly.com.br/app/ask?entry=hero&q=jhyhv+vjh')

html = browser.execute_script("return document.documentElement.outerHTML")
p=[]
soup=BeautifulSoup(html,'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref=link.get('href')
    rt = ('https://brainly.com.br'+str(ref))
    ar = p.append(rt)
    print(ar) 

Everything goes well, with a slight exception.一切都很顺利,只有一个小例外。 When trying to execute the algorithm without using *append* to create the list, it works normally, but when using it, I get an Exit None .当尝试在不使用*append*创建列表的情况下执行算法时,它可以正常工作,但是在使用它时,我得到 Exit None

My Doubt and What I Need to Change To Have a Valid and Orderly Exit in a List !.我的疑问以及我需要更改的内容才能在列表中有效有序地退出!。

Obs:Expected Exit:观察:预期退出:

[' https://link1 ', ' https://link2 '] [' https://link1 ', ' https://link2 ']

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


browser =webdriver.Firefox(executable_path=r'C:/path/geckodriver.exe')
browser.get('https://brainly.com.br/app/ask?entry=hero&q=jhyhv+vjh')

html = browser.execute_script("return document.documentElement.outerHTML")
p=[]
soup=BeautifulSoup(html,'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref=link.get('href')
    rt = ('https://brainly.com.br'+str(ref))
    p.append(rt)
print(p) 

append modifies the list in place, so the variable that has the result is p . append就地修改列表,因此具有结果的变量是p

# (...)

p=[]
soup=BeautifulSoup(html,'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref=link.get('href')
    rt = ('https://brainly.com.br'+str(ref))
    p.append(rt)
print(p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM