这是我脚本的一部分
from bs4 import BeautifulSoup
r = requests.get('https://tap.az/all/consumer-electronics/phones?p%5B749%5D=3860')
soup = BeautifulSoup(r.text, 'html.parser')
results = soup.find_all('div', attrs={'class': 'products-i'})
records = []
for result in results:
model = soup.find('div', attrs={'class': 'products-name'}).text
price = soup.find('span', attrs={'class': 'price-val'}).text + ' AZN'
date_and_place = soup.find('div', attrs={'class': 'products-created'}).text
url = soup.find('a', attrs={'class': 'products-link'}) # NEED UPDATE! URGENT!
records.append((model, price, date_and_place))
print(records)
除了$ 1和$ 2,这一切都很好地呼应了我的脚本。取而代之的是,它输出这些变量的输入,但我希望它从字面上读取“ $ 1”和“ $ 2”。帮助吗?
答案 0 :(得分:3)
转义:
echo "ls /SomeFolder | grep \$1 | xargs cat | grep something | grep .txt | awk '{print \$2}' | sed 's/;\$//';" >> script2.sh
报价:
echo "ls /SomeFolder | grep "'$'"1 | xargs cat | grep something | grep .txt | awk '{print "'$'"2}' | sed 's/;"'$'"//';" >> script2.sh
或类似这样:
echo 'ls /SomeFolder | grep $1 | xargs cat | grep something | grep .txt | awk '\''{print $2}'\'' | sed '\''s/;$//'\'';' >> script2.sh
cat << 'EOF' >> script2.sh
ls /SomeFolder | grep $1 | xargs cat | grep something | grep .txt | awk '{print $2}' | sed 's/;$//';
EOF
基本上,您想防止扩展,即。取字面意义上的字符串。您可能需要阅读bashfaq quotes
答案 1 :(得分:0)
首先,您永远都不会这样写(请参见https://mywiki.wooledge.org/ParsingLs,http://porkmail.org/era/unix/award.html,并且在使用awk时不需要抓地力+吸管+管子):
ls /SomeFolder | grep $1 | xargs cat | grep something | grep .txt | awk '{print $2}' | sed 's/;$//'`
您可以改写为:
find /SomeFolder -mindepth 1 -maxdepth 1 -type f -name "*$1*" -exec \
awk '/something/ && /.txt/{sub(/;$/,"",$2); print $2}' {} +
,或者如果您更喜欢使用print | xargs
而不是-exec
:
find /SomeFolder -mindepth 1 -maxdepth 1 -type f -name "*$1*" -print0 |
xargs -0 awk '/something/ && /.txt/{sub(/;$/,"",$2); print $2}'
现在将该脚本附加到文件将是:
cat <<'EOF' >> script2.sh
find /SomeFolder -mindepth 1 -maxdepth 1 -type f -name "*$1*" -print0 |
xargs -0 awk '/something/ && /.txt/{sub(/;$/,"",$2); print $2}'
EOF
顺便说一句,如果您希望将.
中的.txt
逐字处理,而不是将其视为表示“任何字符”的正则表达式元字符,那么您应该使用\.txt
而不是{{1 }}。