I want to get the same result as assigning a variable and writing it directly in a function.
Currently, I am writing programming to get necessary information from url using bs4.
I was able to get the information I wanted using find_all, but I created a variable so that I could get it with other URL and tag combinations.
However, when I used the created variable as an argument of find_all, I could not get any information.
Is there any difference between writing directly in the argument and using a variable?
I would appreciate it if you could tell me.
Supplementary information (FW/tool version, etc.)
from bs4 import BeautifulSoup import requests home_url = "https://stackoverflow.com/" html = requests.get (home_url) home_tag_info = "class_ = \" P-fvRegist__messageBox \ "" soup = BeautifulSoup (html.text,'html.parser') for tag in soup.find_all (class_=" searchResults_name"): # Does not work below # for tag in soup.find_all (home_tag_info): print (tag.text)
Visual Studio Code
Answer # 1
Specify optional arguments
class_ =Do not set to a variable.
-home_tag_info = "class_ = \" P-fvRegist__messageBox \ "" + home_tag_info = "P-fvRegist__messageBox" -for tag in soup.find_all (class_=" searchResults_name"): -# Does not work below -# for tag in soup.find_all (home_tag_info): + for tag in soup.find_all (class_ = home_tag_info):
Answer # 2
home_tag_info = "P-fvRegist__messageBox \" soup = BeautifulSoup (html.text,'html.parser') for tag in soup.find_all (class_ = home_tag_info): print (tag.text)
Answer # 3
soup.find_all (class_="searchResults_name") # In this case, for the option name class_ as an argument It means that the element # [searchResults_name] is specified. soup.find_all ("class_ = \" P-fvRegist__messageBox \ "") # In this case, I did not specify any options as arguments. # I'm just giving the string [class_=" P-fvRegist__messageBox"].
Answer # 4
I was able to organize my knowledge because I used it without noticing that it was an optional argument.
Thank you for telling me.
- how to save the image ocr result file in python
- please tell me how to write a program to end when the result is executed 5 times [python]
- excel python i want to get the display result instead of the conditional statement
- about for syntax i want to know the reason why the result is different depending on where the initial value is defined python
- python 3x - about assigning python to a two-dimensional list
- python - i want to restore the result of calculating the inner product with numpy
- i want to put it in another variable with python for
- i want to dynamically change the value of a variable in python and execute it
- python - pass a variable to the websocket on_message function
- i want to output the entry result of tkinter with python
- python 3x - i want to divide the process when it is err as a result of command execution in python3 subprocess
- python 3x - i want to save the value of a variable in memory
- about writing if statements in dictionaries with python
- python - next day forecast result display error
- the calculation result of python print (1/2) becomes 0
- [python] when writing a pandas csv file, a comma "," is inserted at the beginning for some reason
- if the value of the variable to which the variable is assigned is changed in python, the value will change to the original varia
- python - filename cannot be assigned to a variable when using a for statement with a shell command in google colaboratory
- python - saving the calculation result of the correlation coefficient
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- xcode - pod install [!] no `podfile 'found in the project directory