英文:
transform lines into a list with python
问题
以下是翻译好的代码部分:
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)
# Obs:Expected Exit:
# ['https://link1', 'https://link2']
希望这有所帮助。
英文:
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
.
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']
答案1
得分: 0
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)
英文:
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)
答案2
得分: 0
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)
英文:
append
modifies the list in place, so the variable that has the result is 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论